mirror of
https://github.com/sasjs/lint.git
synced 2026-01-04 11:20:04 +00:00
chore(*): add tests for new functionality
This commit is contained in:
@@ -8,22 +8,76 @@ describe('formatFile', () => {
|
|||||||
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
|
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
|
||||||
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
|
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
|
||||||
await createFile(path.join(__dirname, 'format-file-test.sas'), content)
|
await createFile(path.join(__dirname, 'format-file-test.sas'), content)
|
||||||
|
const expectedResult = {
|
||||||
|
updatedFilePaths: [path.join(__dirname, 'format-file-test.sas')],
|
||||||
|
fixedDiagnosticsCount: 3,
|
||||||
|
unfixedDiagnostics: []
|
||||||
|
}
|
||||||
|
|
||||||
await formatFile(path.join(__dirname, 'format-file-test.sas'))
|
const result = await formatFile(
|
||||||
const result = await readFile(path.join(__dirname, 'format-file-test.sas'))
|
path.join(__dirname, 'format-file-test.sas')
|
||||||
|
)
|
||||||
|
const formattedContent = await readFile(
|
||||||
|
path.join(__dirname, 'format-file-test.sas')
|
||||||
|
)
|
||||||
|
|
||||||
expect(result).toEqual(expectedContent)
|
expect(result).toEqual(expectedResult)
|
||||||
|
expect(formattedContent).toEqual(expectedContent)
|
||||||
|
|
||||||
await deleteFile(path.join(__dirname, 'format-file-test.sas'))
|
await deleteFile(path.join(__dirname, 'format-file-test.sas'))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should use the provided config if available', async () => {
|
it('should use the provided config if available', async () => {
|
||||||
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
|
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
|
||||||
const expectedContent = `/**\r\n @file\r\n @brief <Your brief here>\r\n <h4> SAS Macros </h4>\r\n**/\r\n%macro somemacro();\r\n%put 'hello';\r\n%mend somemacro;`
|
const expectedContent = `/**\r\n @file\r\n @brief <Your brief here>\r\n <h4> SAS Macros </h4>\r\n**/\r\n%macro somemacro();\r\n%put 'hello';\r\n%mend;`
|
||||||
|
const expectedResult = {
|
||||||
|
updatedFilePaths: [path.join(__dirname, 'format-file-config.sas')],
|
||||||
|
fixedDiagnosticsCount: 2,
|
||||||
|
unfixedDiagnostics: [
|
||||||
|
{
|
||||||
|
endColumnNumber: 7,
|
||||||
|
lineNumber: 8,
|
||||||
|
message: '%mend statement is missing macro name - somemacro',
|
||||||
|
severity: 1,
|
||||||
|
startColumnNumber: 1
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
await createFile(path.join(__dirname, 'format-file-config.sas'), content)
|
await createFile(path.join(__dirname, 'format-file-config.sas'), content)
|
||||||
|
|
||||||
await formatFile(
|
const result = await formatFile(
|
||||||
path.join(__dirname, 'format-file-config.sas'),
|
path.join(__dirname, 'format-file-config.sas'),
|
||||||
|
new LintConfig({
|
||||||
|
lineEndings: 'crlf',
|
||||||
|
hasMacroNameInMend: false,
|
||||||
|
hasDoxygenHeader: true,
|
||||||
|
noTrailingSpaces: true
|
||||||
|
})
|
||||||
|
)
|
||||||
|
const formattedContent = await readFile(
|
||||||
|
path.join(__dirname, 'format-file-config.sas')
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(result).toEqual(expectedResult)
|
||||||
|
expect(formattedContent).toEqual(expectedContent)
|
||||||
|
|
||||||
|
await deleteFile(path.join(__dirname, 'format-file-config.sas'))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not update any files if there are no formatting violations', async () => {
|
||||||
|
const content = `/**\r\n @file\r\n @brief <Your brief here>\r\n <h4> SAS Macros </h4>\r\n**/\r\n%macro somemacro();\r\n%put 'hello';\r\n%mend somemacro;`
|
||||||
|
const expectedResult = {
|
||||||
|
updatedFilePaths: [],
|
||||||
|
fixedDiagnosticsCount: 0,
|
||||||
|
unfixedDiagnostics: []
|
||||||
|
}
|
||||||
|
await createFile(
|
||||||
|
path.join(__dirname, 'format-file-no-violations.sas'),
|
||||||
|
content
|
||||||
|
)
|
||||||
|
|
||||||
|
const result = await formatFile(
|
||||||
|
path.join(__dirname, 'format-file-no-violations.sas'),
|
||||||
new LintConfig({
|
new LintConfig({
|
||||||
lineEndings: 'crlf',
|
lineEndings: 'crlf',
|
||||||
hasMacroNameInMend: true,
|
hasMacroNameInMend: true,
|
||||||
@@ -31,12 +85,13 @@ describe('formatFile', () => {
|
|||||||
noTrailingSpaces: true
|
noTrailingSpaces: true
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
const result = await readFile(
|
const formattedContent = await readFile(
|
||||||
path.join(__dirname, 'format-file-config.sas')
|
path.join(__dirname, 'format-file-no-violations.sas')
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual(expectedContent)
|
expect(result).toEqual(expectedResult)
|
||||||
|
expect(formattedContent).toEqual(content)
|
||||||
|
|
||||||
await deleteFile(path.join(__dirname, 'format-file-config.sas'))
|
await deleteFile(path.join(__dirname, 'format-file-no-violations.sas'))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,23 +6,39 @@ import {
|
|||||||
deleteFolder,
|
deleteFolder,
|
||||||
readFile
|
readFile
|
||||||
} from '@sasjs/utils/file'
|
} from '@sasjs/utils/file'
|
||||||
|
import { Diagnostic, LintConfig } from '../types'
|
||||||
|
|
||||||
describe('formatFolder', () => {
|
describe('formatFolder', () => {
|
||||||
it('should fix linting issues in a given folder', async () => {
|
it('should fix linting issues in a given folder', async () => {
|
||||||
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
|
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
|
||||||
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
|
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
|
||||||
|
const expectedResult = {
|
||||||
|
updatedFilePaths: [
|
||||||
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
|
||||||
|
],
|
||||||
|
fixedDiagnosticsCount: 3,
|
||||||
|
unfixedDiagnostics: new Map<string, Diagnostic[]>([
|
||||||
|
[
|
||||||
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
|
||||||
|
[]
|
||||||
|
]
|
||||||
|
])
|
||||||
|
}
|
||||||
await createFolder(path.join(__dirname, 'format-folder-test'))
|
await createFolder(path.join(__dirname, 'format-folder-test'))
|
||||||
await createFile(
|
await createFile(
|
||||||
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
|
||||||
content
|
content
|
||||||
)
|
)
|
||||||
|
|
||||||
await formatFolder(path.join(__dirname, 'format-folder-test'))
|
const result = await formatFolder(
|
||||||
const result = await readFile(
|
path.join(__dirname, 'format-folder-test')
|
||||||
|
)
|
||||||
|
const formattedContent = await readFile(
|
||||||
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual(expectedContent)
|
expect(formattedContent).toEqual(expectedContent)
|
||||||
|
expect(result).toEqual(expectedResult)
|
||||||
|
|
||||||
await deleteFolder(path.join(__dirname, 'format-folder-test'))
|
await deleteFolder(path.join(__dirname, 'format-folder-test'))
|
||||||
})
|
})
|
||||||
@@ -30,6 +46,29 @@ describe('formatFolder', () => {
|
|||||||
it('should fix linting issues in subfolders of a given folder', async () => {
|
it('should fix linting issues in subfolders of a given folder', async () => {
|
||||||
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
|
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
|
||||||
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
|
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
|
||||||
|
const expectedResult = {
|
||||||
|
updatedFilePaths: [
|
||||||
|
path.join(
|
||||||
|
__dirname,
|
||||||
|
'format-folder-test',
|
||||||
|
'subfolder',
|
||||||
|
'format-folder-test.sas'
|
||||||
|
)
|
||||||
|
],
|
||||||
|
fixedDiagnosticsCount: 3,
|
||||||
|
unfixedDiagnostics: new Map<string, Diagnostic[]>([
|
||||||
|
[
|
||||||
|
path.join(
|
||||||
|
__dirname,
|
||||||
|
'format-folder-test',
|
||||||
|
'subfolder',
|
||||||
|
'format-folder-test.sas'
|
||||||
|
),
|
||||||
|
[]
|
||||||
|
]
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
await createFolder(path.join(__dirname, 'format-folder-test'))
|
await createFolder(path.join(__dirname, 'format-folder-test'))
|
||||||
await createFolder(path.join(__dirname, 'subfolder'))
|
await createFolder(path.join(__dirname, 'subfolder'))
|
||||||
await createFile(
|
await createFile(
|
||||||
@@ -42,8 +81,10 @@ describe('formatFolder', () => {
|
|||||||
content
|
content
|
||||||
)
|
)
|
||||||
|
|
||||||
await formatFolder(path.join(__dirname, 'format-folder-test'))
|
const result = await formatFolder(
|
||||||
const result = await readFile(
|
path.join(__dirname, 'format-folder-test')
|
||||||
|
)
|
||||||
|
const formattedContent = await readFile(
|
||||||
path.join(
|
path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
'format-folder-test',
|
'format-folder-test',
|
||||||
@@ -52,7 +93,135 @@ describe('formatFolder', () => {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual(expectedContent)
|
expect(result).toEqual(expectedResult)
|
||||||
|
expect(formattedContent).toEqual(expectedContent)
|
||||||
|
|
||||||
|
await deleteFolder(path.join(__dirname, 'format-folder-test'))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should use a custom configuration when provided', async () => {
|
||||||
|
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
|
||||||
|
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
|
||||||
|
const expectedResult = {
|
||||||
|
updatedFilePaths: [
|
||||||
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
|
||||||
|
],
|
||||||
|
fixedDiagnosticsCount: 3,
|
||||||
|
unfixedDiagnostics: new Map<string, Diagnostic[]>([
|
||||||
|
[
|
||||||
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
|
||||||
|
[]
|
||||||
|
]
|
||||||
|
])
|
||||||
|
}
|
||||||
|
await createFolder(path.join(__dirname, 'format-folder-test'))
|
||||||
|
await createFile(
|
||||||
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
|
||||||
|
content
|
||||||
|
)
|
||||||
|
|
||||||
|
const result = await formatFolder(
|
||||||
|
path.join(__dirname, 'format-folder-test'),
|
||||||
|
new LintConfig({
|
||||||
|
lineEndings: 'crlf',
|
||||||
|
hasMacroNameInMend: false,
|
||||||
|
hasDoxygenHeader: true,
|
||||||
|
noTrailingSpaces: true
|
||||||
|
})
|
||||||
|
)
|
||||||
|
const formattedContent = await readFile(
|
||||||
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(formattedContent).toEqual(expectedContent)
|
||||||
|
expect(result).toEqual(expectedResult)
|
||||||
|
|
||||||
|
await deleteFolder(path.join(__dirname, 'format-folder-test'))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should fix linting issues in subfolders of a given folder', async () => {
|
||||||
|
const content = `%macro somemacro(); \n%put 'hello';\n%mend;`
|
||||||
|
const expectedContent = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
|
||||||
|
const expectedResult = {
|
||||||
|
updatedFilePaths: [
|
||||||
|
path.join(
|
||||||
|
__dirname,
|
||||||
|
'format-folder-test',
|
||||||
|
'subfolder',
|
||||||
|
'format-folder-test.sas'
|
||||||
|
)
|
||||||
|
],
|
||||||
|
fixedDiagnosticsCount: 3,
|
||||||
|
unfixedDiagnostics: new Map<string, Diagnostic[]>([
|
||||||
|
[
|
||||||
|
path.join(
|
||||||
|
__dirname,
|
||||||
|
'format-folder-test',
|
||||||
|
'subfolder',
|
||||||
|
'format-folder-test.sas'
|
||||||
|
),
|
||||||
|
[]
|
||||||
|
]
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
await createFolder(path.join(__dirname, 'format-folder-test'))
|
||||||
|
await createFolder(path.join(__dirname, 'subfolder'))
|
||||||
|
await createFile(
|
||||||
|
path.join(
|
||||||
|
__dirname,
|
||||||
|
'format-folder-test',
|
||||||
|
'subfolder',
|
||||||
|
'format-folder-test.sas'
|
||||||
|
),
|
||||||
|
content
|
||||||
|
)
|
||||||
|
|
||||||
|
const result = await formatFolder(
|
||||||
|
path.join(__dirname, 'format-folder-test')
|
||||||
|
)
|
||||||
|
const formattedContent = await readFile(
|
||||||
|
path.join(
|
||||||
|
__dirname,
|
||||||
|
'format-folder-test',
|
||||||
|
'subfolder',
|
||||||
|
'format-folder-test.sas'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(result).toEqual(expectedResult)
|
||||||
|
expect(formattedContent).toEqual(expectedContent)
|
||||||
|
|
||||||
|
await deleteFolder(path.join(__dirname, 'format-folder-test'))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not update any files when there are no violations', async () => {
|
||||||
|
const content = `/**\n @file\n @brief <Your brief here>\n <h4> SAS Macros </h4>\n**/\n%macro somemacro();\n%put 'hello';\n%mend somemacro;`
|
||||||
|
const expectedResult = {
|
||||||
|
updatedFilePaths: [],
|
||||||
|
fixedDiagnosticsCount: 0,
|
||||||
|
unfixedDiagnostics: new Map<string, Diagnostic[]>([
|
||||||
|
[
|
||||||
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
|
||||||
|
[]
|
||||||
|
]
|
||||||
|
])
|
||||||
|
}
|
||||||
|
await createFolder(path.join(__dirname, 'format-folder-test'))
|
||||||
|
await createFile(
|
||||||
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas'),
|
||||||
|
content
|
||||||
|
)
|
||||||
|
|
||||||
|
const result = await formatFolder(
|
||||||
|
path.join(__dirname, 'format-folder-test')
|
||||||
|
)
|
||||||
|
const formattedContent = await readFile(
|
||||||
|
path.join(__dirname, 'format-folder-test', 'format-folder-test.sas')
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(formattedContent).toEqual(content)
|
||||||
|
expect(result).toEqual(expectedResult)
|
||||||
|
|
||||||
await deleteFolder(path.join(__dirname, 'format-folder-test'))
|
await deleteFolder(path.join(__dirname, 'format-folder-test'))
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user