Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

fix(utils): glob error is not handled silently #31

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/utils/src/lib/artifact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ jest.mock('./logger');

describe('artifact', () => {
beforeEach(() => {
(glob.create as jest.Mock).mockResolvedValue({ glob: jest.fn().mockResolvedValue(['dis/test1', 'dist/test2']) });
(glob.create as jest.Mock).mockResolvedValue({ glob: jest.fn().mockResolvedValue(['dist/test1', 'dist/test2']) });
});

it('should not upload if no paths', async () => {
await expect(uploadArtifact(glob, 'test', [])).resolves.toBeUndefined();
});

it('should not upload if not found files', async () => {
(glob.create as jest.Mock).mockResolvedValue({ glob: jest.fn().mockResolvedValue([]) });
(glob.create as jest.Mock).mockResolvedValueOnce({ glob: jest.fn().mockResolvedValue([]) });
await expect(uploadArtifact(glob, 'test', ['dist'])).resolves.toBeUndefined();
});

Expand All @@ -38,9 +38,17 @@ describe('artifact', () => {
logger().debugMode = false;
});

it('should fail globbing silently', async () => {
(glob.create as jest.Mock).mockRejectedValueOnce('error');

await uploadArtifact(glob, 'test', ['dist']);

expect(warning).toHaveBeenCalledWith('error');
});

it('should fail uploading silently', async () => {
(aCreate as jest.Mock).mockImplementationOnce(() => ({
uploadArtifact: jest.fn().mockRejectedValue('test'),
uploadArtifact: jest.fn().mockRejectedValue('error'),
}));

await uploadArtifact(glob, 'test', ['dist']);
Expand Down
24 changes: 12 additions & 12 deletions packages/utils/src/lib/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ export async function uploadArtifact(glob: typeof Glob, name: string, paths: str
const globPaths = paths.map((path) => `${path}/*`).join('\n');
debug(`Upload paths: ${globPaths}`);

const files = await glob.create(globPaths).then((glob) => glob.glob());
try {
const files = await glob.create(globPaths).then((glob) => glob.glob());

if (!files.length) {
info(`Couldn't find files to upload in ${paths.join(', ')}`);
return;
}
if (!files.length) {
info(`Couldn't find files to upload in ${paths.join(', ')}`);
return;
}

debug(`Found ${files.length} files to upload`);
debug(`Found ${files.length} files to upload`);

if (logger().debugMode) {
debug(`Debug mode is on, skipping uploading artifacts`);
return;
}
if (logger().debugMode) {
debug(`Debug mode is on, skipping uploading artifacts`);
return;
}

try {
const { failedItems, artifactName, size } = await artifactClient().uploadArtifact(name, files, process.cwd());
debug(`name: ${artifactName}, size: ${size}, failedItems: ${failedItems.join(', ')}`);

debug(`name: ${artifactName}, size: ${size}, failedItems: ${failedItems.join(', ')}`);
success(`Successfully uploaded ${artifactName}`);
return artifactName;
} catch (e) {
Expand Down