Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure parent directory exists before writing log/diagnostics file #1117

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
1 change: 1 addition & 0 deletions node-src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ vi.mock('fs', async (importOriginal) => {
const originalModule = (await importOriginal()) as any;
return {
pathExists: async () => true,
mkdirSync: vi.fn(),
readFileSync: originalModule.readFileSync,
writeFileSync: vi.fn(),
createReadStream: vi.fn(() => mockStatsFile),
Expand Down
12 changes: 8 additions & 4 deletions node-src/lib/log.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from 'chalk';
import debug from 'debug';
import { createWriteStream, rm } from 'fs';
import { createWriteStream, mkdirSync, rm } from 'fs';
import path from 'path';
import stripAnsi from 'strip-ansi';
import { format } from 'util';

Expand Down Expand Up @@ -96,13 +97,16 @@ const fileLogger = {
this.append = () => {};
this.queue = [];
},
initialize(path: string, onError: LogFunction) {
rm(path, { force: true }, (err) => {
initialize(filepath: string, onError: LogFunction) {
rm(filepath, { force: true }, (err) => {
if (err) {
this.disable();
onError(err);
} else {
const stream = createWriteStream(path, { flags: 'a' });
// Ensure the parent directory exists before we create the stream
mkdirSync(path.dirname(filepath), { recursive: true });

const stream = createWriteStream(filepath, { flags: 'a' });
this.append = (...messages: string[]) => {
stream?.write(
messages
Expand Down
30 changes: 28 additions & 2 deletions node-src/lib/writeChromaticDiagnostics.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { describe, expect, it } from 'vitest';
import { mkdirSync } from 'fs';
import jsonfile from 'jsonfile';
import path from 'path';
import { describe, expect, it, vi } from 'vitest';

import { getDiagnostics } from './writeChromaticDiagnostics';
import { createLogger } from './log';
import { getDiagnostics, writeChromaticDiagnostics } from './writeChromaticDiagnostics';

vi.mock('jsonfile');
vi.mock('fs');

describe('getDiagnostics', () => {
it('returns context object', () => {
Expand All @@ -26,3 +33,22 @@ describe('getDiagnostics', () => {
});
});
});

describe('writeChromaticDiagnostics', () => {
it('should create the parent directory if it does not exist', async () => {
const ctx = {
log: createLogger({}),
options: { diagnosticsFile: '/tmp/doesnotexist/diagnostics.json' },
};
await writeChromaticDiagnostics(ctx as any);

expect(mkdirSync).toHaveBeenCalledWith(path.dirname(ctx.options.diagnosticsFile), {
recursive: true,
});
expect(jsonfile.writeFile).toHaveBeenCalledWith(
ctx.options.diagnosticsFile,
expect.any(Object),
expect.any(Object)
);
});
});
5 changes: 5 additions & 0 deletions node-src/lib/writeChromaticDiagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { mkdirSync } from 'fs';
import jsonfile from 'jsonfile';
import path from 'path';

import { Context } from '..';
import wroteReport from '../ui/messages/info/wroteReport';
Expand All @@ -17,6 +19,9 @@ export async function writeChromaticDiagnostics(ctx: Context) {
}

try {
// Ensure the parent directory exists before writing file
mkdirSync(path.dirname(ctx.options.diagnosticsFile), { recursive: true });

await writeFile(ctx.options.diagnosticsFile, getDiagnostics(ctx), { spaces: 2 });
ctx.log.info(wroteReport(ctx.options.diagnosticsFile, 'Chromatic diagnostics'));
} catch (error) {
Expand Down
Loading