diff --git a/test/fixtures/linter/projects/globals/webapp/manifest.json b/test/fixtures/linter/projects/globals/webapp/manifest.json new file mode 100644 index 000000000..28e66e248 --- /dev/null +++ b/test/fixtures/linter/projects/globals/webapp/manifest.json @@ -0,0 +1,6 @@ +{ + "sap.app": { + "id": "com.example.app", + "type": "application" + } +} diff --git a/test/fixtures/linter/projects/globals/webapp/test/globalFactory1.js b/test/fixtures/linter/projects/globals/webapp/test/globalFactory1.js new file mode 100644 index 000000000..ec949fb4d --- /dev/null +++ b/test/fixtures/linter/projects/globals/webapp/test/globalFactory1.js @@ -0,0 +1,4 @@ +function globalFactory() {} +sap.ui.getCore().attachInit(function () { + sap.ui.require([], globalFactory); +}); diff --git a/test/fixtures/linter/projects/globals/webapp/test/globalFactory2.js b/test/fixtures/linter/projects/globals/webapp/test/globalFactory2.js new file mode 100644 index 000000000..9848c63d6 --- /dev/null +++ b/test/fixtures/linter/projects/globals/webapp/test/globalFactory2.js @@ -0,0 +1,4 @@ +function globalFactory() { + +} +sap.ui.getCore().attachInit(function () {}); diff --git a/test/lib/autofix/autofix.projects.ts b/test/lib/autofix/autofix.projects.ts index 58d3fbbb5..71fa29f93 100644 --- a/test/lib/autofix/autofix.projects.ts +++ b/test/lib/autofix/autofix.projects.ts @@ -88,3 +88,33 @@ test.serial("lint: All files of library with sap.ui.core namespace", async (t) = t.true(writeFilePath.startsWith(projectPath), `${writeFilePath} should start with ${projectPath}`); } }); + +test.serial("lint: All files of globals application", async (t) => { + const projectPath = path.join(fixturesProjectsPath, "globals"); + const {linterEngine} = t.context; + + const res = await linterEngine.lint({ + rootDir: projectPath, + fix: true, + }); + + t.snapshot(preprocessLintResultsForSnapshot(res)); + t.truthy(t.context.autofixSpy.callCount >= 1); + let expectedWrites = 0; + for (let i = t.context.autofixSpy.callCount - 1; i >= 0; i--) { + const autofixResult = await t.context.autofixSpy.getCall(i).returnValue; + expectedWrites += autofixResult.size; + const autofixResultEntries = Array.from(autofixResult.entries()); + autofixResultEntries.sort((a, b) => a[0].localeCompare(b[0])); + for (const [filePath, content] of autofixResultEntries) { + t.snapshot(content, `AutofixResult iteration #${i}: ${filePath}`); + } + } + + // Ensure that all files are written using an absolute path within the project + t.is(t.context.writeFileStub.callCount, expectedWrites); + const writeFilePaths = t.context.writeFileStub.args.map((args) => args[0]); + for (const writeFilePath of writeFilePaths) { + t.true(writeFilePath.startsWith(projectPath), `${writeFilePath} should start with ${projectPath}`); + } +}); diff --git a/test/lib/autofix/snapshots/autofix.projects.ts.md b/test/lib/autofix/snapshots/autofix.projects.ts.md index 769ee2b79..03bb63322 100644 --- a/test/lib/autofix/snapshots/autofix.projects.ts.md +++ b/test/lib/autofix/snapshots/autofix.projects.ts.md @@ -425,3 +425,122 @@ Generated by [AVA](https://avajs.dev). warningCount: 0, }, ] + +## lint: All files of globals application + +> Snapshot 1 + + [ + { + coverageInfo: [], + errorCount: 3, + fatalErrorCount: 0, + filePath: 'webapp/test/globalFactory1.js', + messages: [ + { + column: 1, + line: 2, + message: 'Access of global variable \'sap\' (sap.ui.getCore)', + ruleId: 'no-globals', + severity: 2, + ui5TypeInfo: undefined, + }, + { + column: 8, + line: 2, + message: 'Call to deprecated function \'getCore\' (sap.ui.getCore)', + ruleId: 'no-deprecated-api', + severity: 2, + ui5TypeInfo: { + kind: 'Function', + name: 'getCore', + parent: { + kind: 'Namespace', + name: 'ui', + parent: { + kind: 'Namespace', + name: 'sap', + }, + }, + }, + }, + { + column: 18, + line: 2, + message: 'Call to deprecated function \'attachInit\' of class \'Core\'', + ruleId: 'no-deprecated-api', + severity: 2, + ui5TypeInfo: { + kind: 'Method', + name: 'attachInit', + parent: { + kind: 'Class', + name: 'Core', + parent: { + kind: 'Module', + library: 'sap.ui.core', + name: 'sap/ui/core/Core', + }, + }, + }, + }, + ], + warningCount: 0, + }, + { + coverageInfo: [], + errorCount: 3, + fatalErrorCount: 0, + filePath: 'webapp/test/globalFactory2.js', + messages: [ + { + column: 1, + line: 4, + message: 'Access of global variable \'sap\' (sap.ui.getCore)', + ruleId: 'no-globals', + severity: 2, + ui5TypeInfo: undefined, + }, + { + column: 8, + line: 4, + message: 'Call to deprecated function \'getCore\' (sap.ui.getCore)', + ruleId: 'no-deprecated-api', + severity: 2, + ui5TypeInfo: { + kind: 'Function', + name: 'getCore', + parent: { + kind: 'Namespace', + name: 'ui', + parent: { + kind: 'Namespace', + name: 'sap', + }, + }, + }, + }, + { + column: 18, + line: 4, + message: 'Call to deprecated function \'attachInit\' of class \'Core\'', + ruleId: 'no-deprecated-api', + severity: 2, + ui5TypeInfo: { + kind: 'Method', + name: 'attachInit', + parent: { + kind: 'Class', + name: 'Core', + parent: { + kind: 'Module', + library: 'sap.ui.core', + name: 'sap/ui/core/Core', + }, + }, + }, + }, + ], + warningCount: 0, + }, + ] diff --git a/test/lib/autofix/snapshots/autofix.projects.ts.snap b/test/lib/autofix/snapshots/autofix.projects.ts.snap index 87e1532c7..0354b33a9 100644 Binary files a/test/lib/autofix/snapshots/autofix.projects.ts.snap and b/test/lib/autofix/snapshots/autofix.projects.ts.snap differ