Skip to content

test(Autofix Snapshots): Update tests to only display snapshots from last iteration #713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions test/lib/autofix/snapshots/autofix.fixtures.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@ Generated by [AVA](https://avajs.dev).

## General: Configuration.js

> AutofixResult iteration #0: /Configuration.js

`sap.ui.define(["sap/ui/core/Core", "sap/ui/core/Configuration", "sap/base/i18n/Localization", "sap/ui/core/Locale"], function(Core, Configuration, Localization, Locale) {␊
Configuration.setRTL(false); // Requires two iterations␊
Configuration.setRTL(false).setLanguage("en");␊
Localization.setRTL(false);␊
// Currently not fixable:␊
// setRTL is not chainable and setLanguage can't be applied because␊
// its expression contains a non-side-effect-free call expression␊
Configuration.setRTL(false).setLanguage("en");␊
new Locale(Localization.getLanguageTag());␊
});`

> AutofixResult iteration #1: /Configuration.js

`sap.ui.define(["sap/ui/core/Core", "sap/ui/core/Configuration", "sap/base/i18n/Localization", "sap/ui/core/Locale"], function(Core, Configuration, Localization, Locale) {␊
Expand Down
Binary file modified test/lib/autofix/snapshots/autofix.fixtures.ts.snap
Binary file not shown.
16 changes: 9 additions & 7 deletions test/lib/linter/_linterHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,15 @@ function testDefinition(

if (fix) {
t.truthy(t.context.autofixSpy.callCount >= 1);
for (let i = 0; i < t.context.autofixSpy.callCount; i++) {
const autofixResult = await t.context.autofixSpy.getCall(i).returnValue;
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}`);
}
// Get the last autofix call to only compare the end result with the current snapshot.
// - 2 because there is always one call without any changes (no fixes applied)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, and good thinking. There's just one case where this doesn't work: Once we hit the maximum number of iterations (currently 10) and still produce fixes.

Maybe a better approach would be to again, go through all the calls in reverse order, and for the first (actually last) one that contains results, create a snapshot?

Actually thinking of this, if an autofix touches multiple files (e.g. when fixing a whole project), you might need a different number of iterations per file. So always only going for the last iteration might completely miss fixes that have been applied to files in earlier iterations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We then probably also have to adjust:

for (let i = 0; i < t.context.autofixSpy.callCount; 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}`);
}
}

// and we count from 0 here.
const lastAutofixIterationIndex = t.context.autofixSpy.callCount - 2;
const autofixResult = await t.context.autofixSpy.getCall(lastAutofixIterationIndex).returnValue;
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 #${lastAutofixIterationIndex}: ${filePath}`);
}
}

Expand Down