From f5a0e620a8a6bfc1330316c0965cf273395c535d Mon Sep 17 00:00:00 2001 From: Samuel Bodin <1637651+bodinsamuel@users.noreply.github.com> Date: Mon, 6 Nov 2023 21:37:19 +0100 Subject: [PATCH] fix: handle invalid file and log path --- src/analyser/__snapshots__/index.test.ts.snap | 6 +++--- src/provider/base.ts | 2 +- src/provider/fs.ts | 14 ++++++++++---- src/rules/spec/docker/component.ts | 1 - src/rules/spec/githubActions/component.ts | 1 - tests/__fixtures__/nodejs/invalid/package.json | 3 +++ 6 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 tests/__fixtures__/nodejs/invalid/package.json diff --git a/src/analyser/__snapshots__/index.test.ts.snap b/src/analyser/__snapshots__/index.test.ts.snap index a78cd236..18d40574 100644 --- a/src/analyser/__snapshots__/index.test.ts.snap +++ b/src/analyser/__snapshots__/index.test.ts.snap @@ -745,7 +745,7 @@ exports[`analyser > should run correctly 1`] = ` "languages": { "AMPL": 1, "HCL": 1, - "JSON": 1, + "JSON": 2, "YAML": 2, }, "name": "fake", @@ -1155,7 +1155,7 @@ exports[`analyser > should run correctly 2`] = ` "Go": 1, "HCL": 1, "HTML": 1, - "JSON": 4, + "JSON": 5, "SCSS": 1, "TOML": 1, "YAML": 2, @@ -1805,7 +1805,7 @@ exports[`analyser > should run correctly 2`] = ` "Go": 3, "HCL": 2, "HTML": 3, - "JSON": 11, + "JSON": 13, "SCSS": 3, "TOML": 3, "YAML": 4, diff --git a/src/provider/base.ts b/src/provider/base.ts index 4fb19c8a..1504ab05 100644 --- a/src/provider/base.ts +++ b/src/provider/base.ts @@ -7,7 +7,7 @@ export interface ProviderFile { export interface BaseProvider { basePath: string; listDir: (pathRelative: string) => Promise; - open: (path: string) => Promise; + open: (path: string) => Promise; } export const IGNORED_DIVE_PATHS = [ diff --git a/src/provider/fs.ts b/src/provider/fs.ts index fb1a030e..134cf4b2 100644 --- a/src/provider/fs.ts +++ b/src/provider/fs.ts @@ -1,6 +1,8 @@ import fs from 'fs/promises'; import path from 'node:path'; +import { l } from '../common/log.js'; + import type { BaseProvider, ProviderFile } from './base.js'; export interface FSProviderOptions { @@ -33,9 +35,13 @@ export class FSProvider implements BaseProvider { return list; } - async open(pathRelative: string): Promise { - const content = await fs.readFile(pathRelative); - - return content.toString(); + async open(pathRelative: string): Promise { + try { + const content = await fs.readFile(pathRelative); + return content ? content.toString() : null; + } catch (err) { + l.error('Failed to open file', { pathRelative, err }); + return null; + } } } diff --git a/src/rules/spec/docker/component.ts b/src/rules/spec/docker/component.ts index 5887dd23..42b84a50 100644 --- a/src/rules/spec/docker/component.ts +++ b/src/rules/spec/docker/component.ts @@ -23,7 +23,6 @@ export const detectDockerComponent: ComponentMatcher = async ( const content = await provider.open(file.fp); if (!content) { - l.warn('Failed to open Docker file', file.fp); continue; } diff --git a/src/rules/spec/githubActions/component.ts b/src/rules/spec/githubActions/component.ts index 662d6f88..1d6d7d45 100644 --- a/src/rules/spec/githubActions/component.ts +++ b/src/rules/spec/githubActions/component.ts @@ -47,7 +47,6 @@ export const detectGithubActionsComponent: ComponentMatcher = async ( const content = await provider.open(file.fp); if (!content) { - l.warn('Failed to open GitHub Actions file', file.fp); continue; } diff --git a/tests/__fixtures__/nodejs/invalid/package.json b/tests/__fixtures__/nodejs/invalid/package.json new file mode 100644 index 00000000..e62da650 --- /dev/null +++ b/tests/__fixtures__/nodejs/invalid/package.json @@ -0,0 +1,3 @@ +{ + " +}