Skip to content

Commit

Permalink
tests: cover more edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bodinsamuel committed Jun 29, 2023
1 parent 86360e3 commit 65c030d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/rules/spec/nodejs/dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,21 @@ describe('npm', () => {
Array.from(flatten(res, { merge: true }).techs).sort()
).toStrictEqual(match);
});

it('should match nothing', async () => {
const res = await analyser({
provider: new FakeProvider({
paths: {
'/': ['package.json'],
},
files: {
'/package.json': JSON.stringify({ foo: 'bar' }),
},
}),
});
const match: AllowedKeys[] = ['nodejs'];
expect(
Array.from(flatten(res, { merge: true }).techs).sort()
).toStrictEqual(match);
});
});
43 changes: 43 additions & 0 deletions src/rules/spec/terraform/dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ describe('terraform (lockfile)', () => {
Array.from(flatten(res, { merge: true }).techs).sort()
).toStrictEqual(match);
});

it('should match nothing', async () => {
const res = await analyser({
provider: new FakeProvider({
paths: {
'/': ['.terraform.lock.hcl'],
},
files: {
'/.terraform.lock.hcl': '',
},
}),
});

const match: AllowedKeys[] = ['terraform'];
expect(
Array.from(flatten(res, { merge: true }).techs).sort()
).toStrictEqual(match);
});
});

describe('terraform (resource)', () => {
Expand Down Expand Up @@ -101,4 +119,29 @@ describe('terraform (resource)', () => {
Array.from(flatten(res, { merge: true }).techs).sort()
).toStrictEqual(match);
});

it('should match nothing', async () => {
const resource: string[] = [
`
resource "unknown" "foobar" {
name = "hello"
}`,
];

const res = await analyser({
provider: new FakeProvider({
paths: {
'/': ['main.tf'],
},
files: {
'/main.tf': resource.join(''),
},
}),
});

const match: AllowedKeys[] = ['terraform'];
expect(
Array.from(flatten(res, { merge: true }).techs).sort()
).toStrictEqual(match);
});
});
2 changes: 1 addition & 1 deletion src/rules/spec/terraform/lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const detectTerraformLockfile: ComponentMatcher = async (
let json: Record<string, any>;
try {
json = await parse(file.fp, content);
} catch (err) /* istanbul ignore next */ {
} catch (err) {
console.warn('Failed to parse HCL', err);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/spec/terraform/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const detectTerraformResource: ComponentMatcher = async (
let json: Record<string, any>;
try {
json = await parse(file.fp, content);
} catch (err) /* istanbul ignore next */ {
} catch (err) {
console.warn('Failed to parse HCL', err);
continue;
}
Expand Down

0 comments on commit 65c030d

Please sign in to comment.