Skip to content

Commit

Permalink
fix: calculate artifact name without pathname part
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Scheirlinck authored and dominics committed Jan 18, 2022
1 parent f3f5c7d commit 0906954
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
7 changes: 5 additions & 2 deletions src/artifacts/model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import _ from 'lodash';
import mkdirp from 'mkdirp';

Expand All @@ -20,8 +21,10 @@ export class Artifact {
* @param filename A relative path to the artifact file
*/
constructor(readonly filename: string) {
[this.name] = this.filename.split('.');
this.ext = this.filename.split('.').slice(1).join('.'); // Not extname() because that only gives the last ext
const basename = path.basename(filename);

[this.name] = basename.split('.');
this.ext = basename.split('.').slice(1).join('.'); // Not extname() because that only gives the last ext

const envName = _.snakeCase(this.name).toUpperCase();

Expand Down
17 changes: 7 additions & 10 deletions test/artifacts/compression.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import { promises as fs, createReadStream, existsSync } from 'fs';
import { promisify } from 'util';
import rimrafCb from 'rimraf';
import tempy from 'tempy';
Expand Down Expand Up @@ -27,10 +27,10 @@ describe('compression', () => {
it('can download and inflate .tar.lz4 files correctly', async () => {
const artifact = new Artifact('foo.tar.lz4');

await inflator(fs.createReadStream(getFixturePath('foo.tar.lz4')), artifact);
await inflator(createReadStream(getFixturePath('foo.tar.lz4')), artifact);

expect(fs.existsSync(`${dir}`)).toBe(true);
expect(fs.existsSync(`${dir}/foo/bar`)).toBe(true);
expect(existsSync(`${dir}`)).toBe(true);
expect(existsSync(`${dir}/foo/bar`)).toBe(true);
});
});

Expand All @@ -41,14 +41,11 @@ describe('compression', () => {
const artifact = new Artifact(compressed);

const args = await compression.deflate(artifact);

await exec('cat', [getFixturePath('qux.tar'), ...args]);
expect(existsSync(compressed)).toBe(true);

expect(fs.existsSync(compressed)).toBe(true);

await compression.inflate({ input: fs.createReadStream(compressed), artifact, outputPath: dir });

expect(fs.existsSync(`${dir}/qux/quux`)).toBe(true);
await compression.inflate({ input: createReadStream(compressed), artifact, outputPath: dir });
expect(existsSync(`${dir}/qux/quux`)).toBe(true);
});
});
});
24 changes: 17 additions & 7 deletions test/artifacts/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ import { Artifact } from '../../src/artifacts/model';
import { BUILD_ID, BUILD_ID_2 } from '../fixtures';

describe('model Artifact', () => {
it('looks up the right env vars to find the build', () => {
it('looks up the right env vars to find overridden build IDs', () => {
process.env.BUILDKITE_BUILD_ID = BUILD_ID;
process.env.MONOFO_ARTIFACT_NODE_MODULES_BUILD_ID = BUILD_ID_2;

const artifact = new Artifact('node-modules.tar.gz');
expect(artifact.buildId).toBe(BUILD_ID_2);
const hasExtraEnvVar = new Artifact('node-modules.tar.gz');
expect(hasExtraEnvVar.buildId).toBe(BUILD_ID_2);

const artifact2 = new Artifact('node_modules.tar.gz');
expect(artifact2.buildId).toBe(BUILD_ID_2);
const noExtraEnvVar = new Artifact('other.tar.gz');
expect(noExtraEnvVar.buildId).toBe(BUILD_ID);
});

it.each([
['node-modules.tar.gz', 'node-modules', 'tar.gz'],
['/some/path/node-modules.tar.gz', 'node-modules', 'tar.gz'],
['./node-modules.caidx', 'node-modules', 'caidx'],
])('when given file %s produces name %s and ext %s', (filename, name, ext) => {
process.env.BUILDKITE_BUILD_ID = BUILD_ID;
process.env.MONOFO_ARTIFACT_NODE_MODULES_BUILD_ID = BUILD_ID_2;

const artifact3 = new Artifact('other.tar.gz');
expect(artifact3.buildId).toBe(BUILD_ID);
const artifact = new Artifact(filename);
expect(artifact.name).toBe(name);
expect(artifact.ext).toBe(ext);
});
});

0 comments on commit 0906954

Please sign in to comment.