Skip to content

Commit

Permalink
fix(assets): Correct fingerprint with 'exclude'
Browse files Browse the repository at this point in the history
fixes aws#7718
  • Loading branch information
christophgysin committed Apr 30, 2020
1 parent f7d3cd6 commit a8503a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/@aws-cdk/assets/lib/fs/fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ export function fingerprint(fileOrDirectory: string, options: FingerprintOptions
if (exclude.length) {
_hashField(hash, 'options.exclude', JSON.stringify(exclude));
}
_processFileOrDirectory(fileOrDirectory);
const isDir = fs.statSync(fileOrDirectory).isDirectory();
_processFileOrDirectory(fileOrDirectory, isDir);

return hash.digest('hex');

function _processFileOrDirectory(symbolicPath: string, realPath = symbolicPath) {
if (shouldExclude(exclude, symbolicPath)) {
function _processFileOrDirectory(symbolicPath: string, isRootDir: boolean = false, realPath = symbolicPath) {
if (!isRootDir && shouldExclude(exclude, symbolicPath)) {
return;
}

Expand All @@ -50,15 +51,15 @@ export function fingerprint(fileOrDirectory: string, options: FingerprintOptions
const linkTarget = fs.readlinkSync(realPath);
const resolvedLinkTarget = path.resolve(path.dirname(realPath), linkTarget);
if (shouldFollow(follow, rootDirectory, resolvedLinkTarget)) {
_processFileOrDirectory(symbolicPath, resolvedLinkTarget);
_processFileOrDirectory(symbolicPath, false, resolvedLinkTarget);
} else {
_hashField(hash, `link:${relativePath}`, linkTarget);
}
} else if (stat.isFile()) {
_hashField(hash, `file:${relativePath}`, _contentFingerprint(realPath, stat));
} else if (stat.isDirectory()) {
for (const item of fs.readdirSync(realPath).sort()) {
_processFileOrDirectory(path.join(symbolicPath, item), path.join(realPath, item));
_processFileOrDirectory(path.join(symbolicPath, item), false, path.join(realPath, item));
}
} else {
throw new Error(`Unable to hash ${symbolicPath}: it is neither a file nor a directory`);
Expand Down
14 changes: 14 additions & 0 deletions packages/@aws-cdk/assets/test/fs/test.fs-fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ export = {
const f1 = libfs.fingerprint(dir, options1);
const f2 = libfs.fingerprint(dir, options2);

// THEN
test.notDeepEqual(f1, f2);
test.done();
},
'considers negated exclude patterns for fingerprint'(test: Test) {
// GIVEN
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'fingerprint-tests'));
const options = {path: dir, exclude: ['**', '!file.txt'], sourcePath: dir};

// WHEN
const f1 = libfs.fingerprint(dir, options);
fs.writeFileSync(path.join(dir, 'file.txt'), 'data');
const f2 = libfs.fingerprint(dir, options);

// THEN
test.notDeepEqual(f1, f2);
test.done();
Expand Down

0 comments on commit a8503a5

Please sign in to comment.