Skip to content

Commit

Permalink
fix(assets): Correct fingerprint with 'exclude'
Browse files Browse the repository at this point in the history
fixes #7718
  • Loading branch information
christophgysin committed May 5, 2020
1 parent 4a76973 commit 515b8c5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export = {
],
},
'SourceBucketNames': [{
'Ref': 'AssetParameterse9b696b2a8a1f93ea8b8a9ce1e4dd4727f9243eba984e50411ca95c6b03d26b6S3Bucket1A1EC3E9',
'Ref': 'AssetParameters86f8bca4f28a0bcafef0a98fe4cea25c0071aca27401e35cfaecd06313373bcaS3BucketB41AE64D',
}],
'SourceObjectKeys': [{
'Fn::Join': [
Expand All @@ -241,7 +241,7 @@ export = {
'Fn::Split': [
'||',
{
'Ref': 'AssetParameterse9b696b2a8a1f93ea8b8a9ce1e4dd4727f9243eba984e50411ca95c6b03d26b6S3VersionKeyE46A4824',
'Ref': 'AssetParameters86f8bca4f28a0bcafef0a98fe4cea25c0071aca27401e35cfaecd06313373bcaS3VersionKeyF3CBA38F',
},
],
},
Expand All @@ -254,7 +254,7 @@ export = {
'Fn::Split': [
'||',
{
'Ref': 'AssetParameterse9b696b2a8a1f93ea8b8a9ce1e4dd4727f9243eba984e50411ca95c6b03d26b6S3VersionKeyE46A4824',
'Ref': 'AssetParameters86f8bca4f28a0bcafef0a98fe4cea25c0071aca27401e35cfaecd06313373bcaS3VersionKeyF3CBA38F',
},
],
},
Expand Down
11 changes: 6 additions & 5 deletions packages/@aws-cdk/core/lib/fs/fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,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 @@ -49,15 +50,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/core/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 = FileSystem.fingerprint(dir, options1);
const f2 = FileSystem.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 = FileSystem.fingerprint(dir, options);
fs.writeFileSync(path.join(dir, 'file.txt'), 'data');
const f2 = FileSystem.fingerprint(dir, options);

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

0 comments on commit 515b8c5

Please sign in to comment.