Skip to content
Closed
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
39 changes: 27 additions & 12 deletions ng-dev/ts-circular-dependencies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,6 @@ export function main(

Log.info(green(` Current number of cycles: ${yellow(cycles.length.toString())}`));

if (goldenFile && approve) {
writeFileSync(goldenFile, JSON.stringify(actual, null, 2));
Log.info(green('✔ Updated golden file.'));
return 0;
} else if (!goldenFile) {
Log.error(`x Circular dependency goldens are not allowed.`);
return 1;
} else if (!existsSync(goldenFile)) {
Log.error(`x Could not find golden file: ${goldenFile}`);
return 1;
}

const warningsCount = analyzer.unresolvedFiles.size + analyzer.unresolvedModules.size;

// By default, warnings for unresolved files or modules are not printed. This is because
Expand All @@ -119,6 +107,33 @@ export function main(
Log.warn(` Please rerun with "--warnings" to inspect unresolved imports.`);
}

if (goldenFile === undefined) {
if (approve) {
Log.error(
`x Cannot approve circular depdencies within this repository as no golden file exists.`,
);
return 1;
}
if (cycles.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be outside? What if there is a goldenFile and cycles?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is within the goldenFile being undefined so there is no golden file.

Copy link
Member

Choose a reason for hiding this comment

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

But shouldn't this fail? if there are cycles and goldenFile being !== undefined?

Copy link
Member

Choose a reason for hiding this comment

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

I guess it will happen automatically below with the comparison. That's the thing I missed. Very confusing :D

Copy link
Member Author

Choose a reason for hiding this comment

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

For the case where there is no golden file, and there are cycles here. This fails as it returns a 1 as the exit code.

For cases where there is a golden file to do the comparison to later, we check the specifics.

Log.error(`x No circular dependencies are allow within this repository.`);
return 1;
}

Log.info(green('✔ No circular dependencies found in this repository.'));
return 0;
}

if (approve) {
writeFileSync(goldenFile, JSON.stringify(actual, null, 2));
Log.info(green('✔ Updated golden file.'));
return 0;
}

if (!existsSync(goldenFile)) {
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't this always exist after writing before?

Copy link
Member Author

Choose a reason for hiding this comment

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

The file would only be guaranteed to be written to, if an approval happened. If its a regular check, then it wouldn't be written in the block above, and could technically not exist. We verify it exists because just below here we read from it to do the actual comparison.

Log.error(`x Could not find golden file: ${goldenFile}`);
return 1;
}

const expected = goldenFile ? (JSON.parse(readFileSync(goldenFile, 'utf8')) as Golden) : [];
const {fixedCircularDeps, newCircularDeps} = compareGoldens(actual, expected);
const isMatching = fixedCircularDeps.length === 0 && newCircularDeps.length === 0;
Expand Down
Loading