Skip to content

Commit

Permalink
Fix zip upload and add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Oct 14, 2023
1 parent a682be9 commit 9db311c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion node-src/lib/compress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ afterEach(() => {
});

const testContext = { sourceDir: '/chromatic-tmp', log: new TestLogger() } as any;
const fileInfo = { paths: ['file1'] };
const fileInfo = { paths: ['/chromatic-tmp/file1'] };

describe('makeZipFile', () => {
it('adds files to an archive', async () => {
Expand Down
6 changes: 2 additions & 4 deletions node-src/lib/compress.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import archiver from 'archiver';
import { createReadStream, createWriteStream } from 'fs';
import { join } from 'path';
import { file as tempFile } from 'tmp-promise';
import { Context } from '../types';

Expand All @@ -25,9 +24,8 @@ export default async function makeZipFile(ctx: Context, fileInfo: { paths: strin
archive.pipe(sink);

paths.forEach((path) => {
const fullPath = join(ctx.sourceDir, path);
ctx.log.debug({ fullPath }, 'Adding file to zip archive');
archive.append(createReadStream(fullPath), { name: path });
ctx.log.debug({ path }, 'Adding file to zip archive');
archive.append(createReadStream(path), { name: path });
});

ctx.log.debug('Finalizing zip archive');
Expand Down
58 changes: 58 additions & 0 deletions node-src/tasks/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import { createReadStream, readdirSync, readFileSync, statSync } from 'fs';
import progressStream from 'progress-stream';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { default as compress } from '../lib/compress';
import { getDependentStoryFiles as getDepStoryFiles } from '../lib/getDependentStoryFiles';
import { findChangedDependencies as findChangedDep } from '../lib/findChangedDependencies';
import { findChangedPackageFiles as findChangedPkg } from '../lib/findChangedPackageFiles';
import { validateFiles, traceChangedFiles, uploadStorybook } from './upload';

vi.mock('fs');
vi.mock('progress-stream');
vi.mock('../lib/compress');
vi.mock('../lib/getDependentStoryFiles');
vi.mock('../lib/findChangedDependencies');
vi.mock('../lib/findChangedPackageFiles');
vi.mock('./read-stats-file');

const makeZipFile = vi.mocked(compress);
const findChangedDependencies = vi.mocked(findChangedDep);
const findChangedPackageFiles = vi.mocked(findChangedPkg);
const getDependentStoryFiles = vi.mocked(getDepStoryFiles);
Expand Down Expand Up @@ -372,4 +375,59 @@ describe('uploadStorybook', () => {
unit: 'bytes',
});
});

describe('with zip', () => {
it.only('retrieves the upload location, adds the files to an archive and uploads it', async () => {
const client = { runQuery: vi.fn() };
client.runQuery.mockReturnValue({
getZipUploadUrl: {
domain: 'https://asdqwe.chromatic.com',
url: 'https://asdqwe.chromatic.com/storybook.zip',
sentinelUrl: 'https://asdqwe.chromatic.com/upload.txt',
},
});

makeZipFile.mockReturnValue(Promise.resolve({ path: 'storybook.zip', size: 80 }));
createReadStreamMock.mockReturnValue({ pipe: vi.fn() } as any);
http.fetch.mockReturnValue({ ok: true, text: () => Promise.resolve('OK') });
progress.mockReturnValue({ on: vi.fn() } as any);

const fileInfo = {
lengths: [
{ knownAs: 'iframe.html', contentLength: 42 },
{ knownAs: 'index.html', contentLength: 42 },
],
paths: ['iframe.html', 'index.html'],
total: 84,
};
const ctx = {
client,
env,
log,
http,
sourceDir: '/static/',
options: { zip: true },
fileInfo,
announcedBuild: { id: '1' },
} as any;
await uploadStorybook(ctx, {} as any);

expect(client.runQuery).toHaveBeenCalledWith(
expect.stringMatching(/GetZipUploadUrlMutation/),
{ buildId: '1' }
);
expect(http.fetch).toHaveBeenCalledWith(
'https://asdqwe.chromatic.com/storybook.zip',
expect.objectContaining({
method: 'PUT',
headers: {
'content-type': 'application/zip',
'content-length': '80',
},
}),
expect.objectContaining({ retries: 0 })
);
expect(ctx.uploadedBytes).toBe(80);
});
});
});

0 comments on commit 9db311c

Please sign in to comment.