Skip to content

Commit

Permalink
chore: remove mock-fs from tests
Browse files Browse the repository at this point in the history
It has some nasty, hard-to-fix bugs in current versions of Node.js (tschaub/mock-fs#377, tschaub/mock-fs#380) and an uncertain future (tschaub/mock-fs#383).

Also, this package's tests don't really need to mock the entire filesystem; they just need to write files to a temp directory that can be deleted when the tests are complete.
  • Loading branch information
hegemonic committed Dec 20, 2023
1 parent 0b53114 commit 5197a42
Show file tree
Hide file tree
Showing 22 changed files with 306 additions and 217 deletions.
16 changes: 5 additions & 11 deletions lib/tasks/copy-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,11 @@ export default class CopyFiles extends Task {
tickets.forEach((ticket) => {
const outputFile = path.join(ctx.destination, ticket.url);
const outputDir = path.dirname(outputFile);
let promise = Promise.resolve();

promise = promise
.then(
() => ensureDir(outputDir),
(e) => Promise.reject(e)
)
.then(
() => copyFile(ticket.source, outputFile),
(e) => Promise.reject(e)
);
const promise = ensureDir(outputDir).then(
() => copyFile(ticket.source, outputFile),
(e) => Promise.reject(e)
);

promises.push(promise);
});

Expand Down
22 changes: 10 additions & 12 deletions lib/tasks/generate-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ export default class GenerateFiles extends Task {
const options = {
beautify,
};
let output;
const outputFile = path.join(ctx.destination, ticket.url);
const outputDir = path.dirname(outputFile);
let promise = Promise.resolve();
let promise;

ticket.data.href = ticket.url;
if (!ticket.data.package) {
Expand All @@ -55,16 +54,15 @@ export default class GenerateFiles extends Task {
options.beautify = false;
}

// TODO: Replace with async method.
ensureDir.sync(outputDir);
promise = promise.then(
async () => {
output = await template.render(ticket.viewName, ticket.data, options);

return writeFile(outputFile, output);
},
(e) => Promise.reject(e)
);
promise = ensureDir(outputDir)
.then(
() => template.render(ticket.viewName, ticket.data, options),
(e) => Promise.reject(e)
)
.then(
(output) => writeFile(outputFile, output),
(e) => Promise.reject(e)
);

promises.push(promise);
});
Expand Down
3 changes: 2 additions & 1 deletion lib/tasks/generate-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ export default class GenerateIndex extends GenerateFiles {

async run(ctx) {
try {
const readme = ctx.readme ? await readFile(ctx.readme, ctx.template.encoding) : null;
const data = {
allLongnamesTree: ctx.allLongnamesTree,
package: ctx.package,
pageTitle: ctx.template.translate('pageTitleNoCategory', {
prefix: ctx.pageTitlePrefix,
title: ctx.template.translate('brandDefault'),
}),
readme: ctx.readme ? await readFile(ctx.readme, ctx.template.encoding) : null,
readme,
};

this.url ??= ctx.linkManager.getUri('index');
Expand Down
16 changes: 0 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
"jasmine": "^5.1.0",
"jasmine-console-reporter": "^3.1.0",
"license-check-and-add": "^4.0.5",
"mock-fs": "^5.2.0",
"parcel": "^2.10.3",
"parcel-namer-rewrite": "^2.0.0-rc.3",
"prettier": "^3.1.1",
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/tasks/copy-files/bar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2023 the Baseline Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/* bar baz qux */
17 changes: 17 additions & 0 deletions test/fixtures/tasks/copy-files/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2023 the Baseline Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// foo bar baz
17 changes: 17 additions & 0 deletions test/fixtures/tasks/copy-static-files/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2023 the Baseline Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

foo
17 changes: 17 additions & 0 deletions test/fixtures/tasks/copy-static-files/foo/bar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2023 the Baseline Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

bar
17 changes: 17 additions & 0 deletions test/fixtures/tasks/copy-static-files/foo/bar/baz.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2023 the Baseline Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

baz
17 changes: 17 additions & 0 deletions test/fixtures/tasks/generate-index/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
Copyright 2023 the Baseline Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

Hello, world!
17 changes: 17 additions & 0 deletions test/fixtures/tasks/generate-source-files/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2023 the Baseline Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

exports.bar = () => 2 < 3;
17 changes: 17 additions & 0 deletions test/fixtures/tasks/generate-source-files/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2023 the Baseline Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

exports.foo = 1;
13 changes: 11 additions & 2 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

// Helper functions for testing the Baseline template.
import mock from 'mock-fs'; // eslint-disable-line

import fs from 'node:fs';
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

Expand Down Expand Up @@ -227,6 +227,15 @@ global.helpers = helpers = {
// Sets up the runtime environment so that JSDoc can work properly.
setup: resetJsdocEnv,

tmpdir: async () => {
const tmp = await mkdtemp(path.join(tmpdir(), 'jsdoc-'));

return {
reset: () => rm(tmp, { recursive: true }),
tmp,
};
},

// Converts a class instance to a dictionary-like object, so that `toEqual()` works.
toObject: (instance) => JSON.parse(JSON.stringify(instance)),
};
Expand Down
39 changes: 19 additions & 20 deletions test/specs/lib/tasks/copy-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
limitations under the License.
*/

// eslint-disable-next-line simple-import-sort/imports
import mock from 'mock-fs';

import { readFile, stat } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { loadConfigSync } from '../../../../lib/config.js';
import CopyFiles from '../../../../lib/tasks/copy-files.js';
import Ticket from '../../../../lib/ticket.js';

const SOURCE_DIR = 'sourcefiles';
const __dirname = path.dirname(fileURLToPath(import.meta.url));

const SOURCE_DIR = path.resolve(__dirname, '../../../fixtures/tasks/copy-files');
const OUTPUT_DIR = 'out';
const TYPE_ERROR = 'TypeError';

Expand Down Expand Up @@ -84,24 +84,23 @@ describe('lib/tasks/copy-files', () => {
let conf;
let context;
let result;
let tmp;
let tmpdir;

beforeEach(() => {
beforeEach(async () => {
conf = loadConfigSync(helpers.deps);
context = {
destination: OUTPUT_DIR,
destination: null,
templateConfig: conf,
};

mock({
[SOURCE_DIR]: {
'foo.js': 'foo bar baz',
'bar.css': 'bar baz qux',
},
});
tmpdir = await helpers.tmpdir();
tmp = tmpdir.tmp;
context.destination = path.join(tmp, OUTPUT_DIR);
});

afterEach(() => {
mock.restore();
afterEach(async () => {
await tmpdir.reset();
});

it('returns a promise on success', (cb) => {
Expand Down Expand Up @@ -237,9 +236,9 @@ describe('lib/tasks/copy-files', () => {
});

await task.run(context);
file = await readFile(path.join(OUTPUT_DIR, url), 'utf8');
file = await readFile(path.join(context.destination, url), 'utf8');

expect(file).toBe('foo bar baz');
expect(file).toContain('foo bar baz');
});

it('saves files for multiple tickets in the right places', async () => {
Expand Down Expand Up @@ -278,9 +277,9 @@ describe('lib/tasks/copy-files', () => {
});

await task.run(context);
file = await readFile(path.join(OUTPUT_DIR, url), 'utf8');
file = await readFile(path.join(context.destination, url), 'utf8');

expect(file).toBe('foo bar baz');
expect(file).toContain('foo bar baz');
});

it('works when tickets are added after calling the constructor', async () => {
Expand All @@ -296,9 +295,9 @@ describe('lib/tasks/copy-files', () => {

task.tickets = [ticket];
await task.run(context);
file = await readFile(path.join(OUTPUT_DIR, url), 'utf8');
file = await readFile(path.join(context.destination, url), 'utf8');

expect(file).toBe('foo bar baz');
expect(file).toContain('foo bar baz');
});
});
});
Expand Down
Loading

0 comments on commit 5197a42

Please sign in to comment.