Skip to content

Commit

Permalink
Fix: check if an existing file is a symlink during symlink testing (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm committed Mar 18, 2024
1 parent 02ca43e commit 77601c7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/modules/candidateWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,10 @@ export default class CandidateWriter extends Module {
return 'doesn\'t exist';
}

if (!await fsPoly.isSymlink(linkPath)) {
return 'is not a symlink';
}

const existingSourcePath = await fsPoly.readlink(linkPath);
if (path.normalize(existingSourcePath) !== path.normalize(expectedTargetPath)) {
return `has the target path '${existingSourcePath}', expected '${expectedTargetPath}`;
Expand Down
44 changes: 44 additions & 0 deletions test/modules/candidateWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,50 @@ describe.each([
await expect(walkAndStat(inputTemp)).resolves.toMatchObject(inputFilesBefore);
});
});

it('should write if the output is not expected and overwriting invalid', async () => {
await copyFixturesToTemp(async (inputTemp, outputTemp) => {
// Given
const options = new Options({ commands: ['link', 'test'], symlink });
const inputFilesBefore = await walkAndStat(inputTemp);
await expect(walkAndStat(outputTemp)).resolves.toHaveLength(0);

// And we've written once
await candidateWriter(options, inputTemp, '**/*', undefined, outputTemp);

// And files were written
const outputFilesBefore = await walkAndStat(outputTemp);
expect(outputFilesBefore).not.toHaveLength(0);
for (const [, stats] of outputFilesBefore) {
expect(stats.isSymbolicLink()).toEqual(symlink);
}

// And the files are made invalid
await Promise.all(outputFilesBefore.map(async ([filePath]) => {
const resolvedPath = path.join(outputTemp, filePath);
await fsPoly.rm(resolvedPath);
await fsPoly.touch(resolvedPath);
}));

// When we write again
await candidateWriter({
...options,
overwriteInvalid: true,
}, inputTemp, '**/*', undefined, outputTemp);

// Then the output was touched
const outputFilesAfter = await walkAndStat(outputTemp);
expect(outputFilesAfter.map((pair) => pair[0]))
.toEqual(outputFilesBefore.map((pair) => pair[0]));
expect(outputFilesAfter).not.toEqual(outputFilesBefore);
for (const [, stats] of outputFilesAfter) {
expect(stats.isSymbolicLink()).toEqual(symlink);
}

// And the input files weren't touched
await expect(walkAndStat(inputTemp)).resolves.toMatchObject(inputFilesBefore);
});
});
});

it('should write relative symlinks', async () => {
Expand Down

0 comments on commit 77601c7

Please sign in to comment.