Skip to content

Commit

Permalink
populate-files: Add unit test with full test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLey committed Dec 13, 2024
1 parent b69df1e commit 51d0c08
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/chilled-rats-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"populate-files": patch
---

Add unit tests w/full test coverage
46 changes: 46 additions & 0 deletions apps/populate-files/populate-files/.c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"reporter": [
"html",
"text-summary"
],
"exclude": [
"coverage/**",
"eslint.config.js",
"src/tests/**"
],
"exclude-after-remap": true,
"extension": [
".js",
".cjs",
".mjs",
".ts",
".cts",
".mts",
".tsx",
".jsx"
],

"lines": 100,
"statements": 100,
"functions": 100,
"branches": 100,

"watermarks": {
"lines": [
100,
95
],
"functions": [
100,
95
],
"branches": [
100,
95
],
"statements": [
100,
95
]
}
}
2 changes: 2 additions & 0 deletions apps/populate-files/populate-files/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"npm-update-ts-references": {},
"tsc": {},
"coverage-reset": {},
"mocha-unit-test": {},
"mocha-integration-test": {},
"coverage-report": {},
"analyze:_": {},
"analyze:format": {},
"analyze:lint": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ suite('Integration test', () => {
expect(data).to.equal(prettyJson(content));
});

withTmpFiles.test('Updates file out of sync', async ctx => {
withTmpFiles.test('Updates file out of sync, then skips future updates', async ctx => {
const content = new Uint8Array([12, 34, 56, 78, 90]);

expect(
Expand All @@ -78,6 +78,19 @@ suite('Integration test', () => {

const data = await readFile(ctx.tmpJsonFile.path);
expect(new Uint8Array(data)).to.deep.equal(content);

expect(
await populateFile(
{
filePath: ctx.tmpJsonFile.path,
content,
},
{ check: false }
)
).to.deep.equal({
updated: false,
filePath: ctx.tmpJsonFile.path,
});
});

withTmpFiles.test('Does not update file during dry run', async ctx => {
Expand Down Expand Up @@ -120,6 +133,29 @@ suite('Integration test', () => {
const data = await readFile(ctx.tmpJsonFile.path, 'utf8');
expect(data).to.not.deep.equal(content);
});

withTmpFiles.test('Errors when file is created during check', async ctx => {
const content = 'File will not exist';
const txtPath = Path.join(ctx.tmpDir.path, 'new-txt-file.txt');

await expect(
populateFile(
{
filePath: txtPath,
content: Promise.resolve(content),
},
{ check: true, dryRun: true }
)
).to.eventually.be.rejectedWith(
Error,
`File ${txtPath} not up to date. Reason: file-not-exist`
);

await expect(readFile(txtPath, 'utf8')).to.eventually.be.rejectedWith(
Error,
'ENOENT: no such file or director'
);
});
});

suite('populateFiles', () => {
Expand Down Expand Up @@ -161,6 +197,31 @@ suite('Integration test', () => {
]);
expect(jsonData).to.equal(prettyJson(content));
expect(txtData).to.equal(content.foo);

expect(
await populateFiles(
[
{
filePath: jsonFilePath,
content,
},
{
filePath: txtFilePath,
content: content.foo,
},
],
{ check: false }
)
).to.deep.equal([
{
updated: false,
filePath: jsonFilePath,
},
{
updated: false,
filePath: txtFilePath,
},
]);
});

withTmpFiles.test('Updates file out of sync', async ctx => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { suite, test } from 'mocha-chain';

suite('types', () => {
test('coverage', async () => {
await import('../../../../lib/lib/types.js');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import type { TextFormatter } from 'format-file';
import { beforeEach, suite } from 'mocha-chain';
import type { ParseCwd } from 'parse-cwd';
import { stubMethod } from 'sinon-typed-stub';
import { Normalize } from '../../../lib/normalize.js';
import { expect } from '../../chai-hooks.js';

suite('Normalize', () => {
const withStubs = beforeEach(() => {
const stubbedTextFormatter = stubMethod<TextFormatter>();
const stubbedParseCwd = stubMethod<ParseCwd>();
return {
stubbedTextFormatter: stubbedTextFormatter.stub,
stubbedParseCwd: stubbedParseCwd.stub,
normalize: new Normalize(true, stubbedParseCwd.method, stubbedTextFormatter.method),
};
});

suite('normalizeFileParams', () => {
withStubs.test('success', async ctx => {
ctx.stubbedParseCwd.withArgs('<cwd>').resolves('/root/dir');

expect(
await ctx.normalize.normalizeFileParams(
{
filePath: 'file/path',
content: '<content>',
},
{
dryRun: true,
check: false,
cwd: '<cwd>',
}
)
).to.deep.equal({
filePath: '/root/dir/file/path',
content: new Uint8Array([60, 99, 111, 110, 116, 101, 110, 116, 62]),
dryRun: true,
check: false,
});

expect(ctx.stubbedTextFormatter.called).to.equal(false);
});

withStubs.test('Options are optional', async ctx => {
ctx.stubbedParseCwd.resolves('/root/dir');
ctx.stubbedTextFormatter
.withArgs('{"foo":"bar"}', { ext: '.json' })
.resolves('foo-bar');

expect(
await ctx.normalize.normalizeFileParams({
filePath: '/file/path',
content: Promise.resolve({ foo: 'bar' }),
})
).to.deep.equal({
filePath: '/file/path',
content: new Uint8Array([102, 111, 111, 45, 98, 97, 114]),
dryRun: false,
check: true,
});
});
});

suite('normalizeFilesParams', () => {
withStubs.test('success', async ctx => {
ctx.stubbedParseCwd.withArgs('<cwd>').resolves('/root/dir');

expect(
await ctx.normalize.normalizeFilesParams(
[
{
filePath: 'file/path/1',
content: '<content>',
},
{
filePath: '/file/path/2',
content: new Uint8Array([1, 2, 3, 4]),
},
],
{
dryRun: true,
check: false,
cwd: '<cwd>',
}
)
).to.deep.equal({
files: [
{
filePath: '/root/dir/file/path/1',
content: new Uint8Array([60, 99, 111, 110, 116, 101, 110, 116, 62]),
},
{
filePath: '/file/path/2',
content: new Uint8Array([1, 2, 3, 4]),
},
],
dryRun: true,
check: false,
});

expect(ctx.stubbedTextFormatter.called).to.equal(false);
});

withStubs.test('Options are optional', async ctx => {
ctx.stubbedParseCwd.resolves('/root/dir');
ctx.stubbedTextFormatter
.withArgs('{"foo":"bar"}', { ext: '.json' })
.resolves('foo-bar');

expect(
await ctx.normalize.normalizeFilesParams([
{ filePath: 'file/path/1', content: { foo: 'bar' } },
{ filePath: '/file/path/2', content: Promise.resolve('<data>') },
])
).to.deep.equal({
files: [
{
filePath: '/root/dir/file/path/1',
content: new Uint8Array([102, 111, 111, 45, 98, 97, 114]),
},
{
filePath: '/file/path/2',
content: new Uint8Array([60, 100, 97, 116, 97, 62]),
},
],
dryRun: false,
check: true,
});
});
});
});

0 comments on commit 51d0c08

Please sign in to comment.