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

chore: remove mock-fs, to run unit tests in nodefs v20 #1208

Merged
merged 2 commits into from
Apr 26, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node_version: ['14', '16', '18']
node_version: ['18', '20']
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ dist
/coverage
/package-lock.json
/yarn.lock
.npmrc
/tmpdir*
.npmrc
27 changes: 15 additions & 12 deletions lib/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ exports.couldMissGulpPreprocess = function(id) {
return ext && ext !== '.js' && ext !== '.html' && ext !== '.css';
};

// require.resolve(packageName) cannot resolve package has no main.
// for instance: font-awesome v4.7.0
// manually try resolve paths
const PACKAGE_PATHS = [
// normal search from cli
...require.resolve.paths('not-core/'),
// additional search from app's folder, this is necessary to support
// lerna hoisting where cli is out of app's local node_modules folder.
...require('resolve/lib/node-modules-paths')(process.cwd(), {})
];
function getPackagePaths() {
// require.resolve(packageName) cannot resolve package has no main.
// for instance: font-awesome v4.7.0
// manually try resolve paths
return [
// normal search from cli
...require.resolve.paths('not-core/'),
// additional search from app's folder, this is necessary to support
// lerna hoisting where cli is out of app's local node_modules folder.
...require('resolve/lib/node-modules-paths')(process.cwd(), {})
];
}

// resolve npm package path
exports.resolvePackagePath = function(packageName) {
for (let i = 0, len = PACKAGE_PATHS.length; i < len; i++) {
const dirname = path.join(PACKAGE_PATHS[i], packageName);
const packagePaths = getPackagePaths();
for (let i = 0, len = packagePaths.length; i < len; i++) {
const dirname = path.join(packagePaths[i], packageName);
if (fs.isDirectory(dirname)) return dirname;
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
"eslint": "^8.17.0",
"jasmine": "^4.1.0",
"jasmine-spec-reporter": "^7.0.0",
"mock-fs": "^5.1.2",
"nodemon": "^2.0.16",
"nyc": "^15.1.0",
"standard-changelog": "^2.0.27",
Expand Down
3 changes: 1 addition & 2 deletions spec/lib/build/bundler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ const path = require('path');
const Bundler = require('../../../lib/build/bundler').Bundler;
const PackageAnalyzer = require('../../mocks/package-analyzer');
const CLIOptionsMock = require('../../mocks/cli-options');
const mockfs = require('../../mocks/mock-fs');

describe('the Bundler module', () => {
let analyzer;
let cliOptionsMock;
let mockfs;

beforeEach(() => {
mockfs = require('mock-fs');
analyzer = new PackageAnalyzer();
cliOptionsMock = new CLIOptionsMock();
cliOptionsMock.attach();
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/build/dependency-inclusion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const BundlerMock = require('../../mocks/bundler');
const SourceInclusion = require('../../../lib/build/source-inclusion').SourceInclusion;
const DependencyInclusion = require('../../../lib/build/dependency-inclusion').DependencyInclusion;
const DependencyDescription = require('../../../lib/build/dependency-description').DependencyDescription;
const mockfs = require('mock-fs');
const mockfs = require('../../mocks/mock-fs');
const Minimatch = require('minimatch').Minimatch;
const path = require('path');

Expand Down
3 changes: 1 addition & 2 deletions spec/lib/build/find-deps.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fd = require('../../../lib/build/find-deps');
const mockfs = require('../../mocks/mock-fs');
const findJsDeps = fd.findJsDeps;
const findHtmlDeps = fd.findHtmlDeps;
const findDeps = fd.findDeps;
Expand Down Expand Up @@ -52,10 +53,8 @@ let css = `
`;

describe('find-deps', () => {
let mockfs;

beforeEach(() => {
mockfs = require('mock-fs');
mockfs({});
});

Expand Down
4 changes: 1 addition & 3 deletions spec/lib/build/package-analyzer.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const path = require('path');
const mockfs = require('../../mocks/mock-fs');
const PackageAnalyzer = require('../../../lib/build/package-analyzer').PackageAnalyzer;

describe('The PackageAnalyzer', () => {
let mockfs;
let project;
let sut;

beforeEach(() => {
mockfs = require('mock-fs');

project = {
paths: {
root: './src/'
Expand Down
8 changes: 2 additions & 6 deletions spec/lib/build/package-installer.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const mockfs = require('../../mocks/mock-fs');
const PackageInstaller = require('../../../lib/build/package-installer').PackageInstaller;
const path = require('path');

describe('The PackageInstaller', () => {
let mockfs;
let project;
let sut;

describe('when there is no yarn.lock file', () => {
beforeEach(() => {
mockfs = require('mock-fs');
project = {};
sut = new PackageInstaller(project);
const fsConfig = {};
Expand Down Expand Up @@ -36,11 +34,9 @@ describe('The PackageInstaller', () => {

describe('when there is yarn.lock file', () => {
beforeEach(() => {
mockfs = require('mock-fs');
project = {};
sut = new PackageInstaller(project);
const fsConfig = {};
fsConfig[path.resolve(process.cwd(), 'yarn.lock')] = 'some-content';
const fsConfig = { 'yarn.lock': 'some-content'};
mockfs(fsConfig);
});

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/build/source-inclusion.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const BundlerMock = require('../../mocks/bundler');
const SourceInclusion = require('../../../lib/build/source-inclusion').SourceInclusion;
const mockfs = require('mock-fs');
const mockfs = require('../../mocks/mock-fs');
const Minimatch = require('minimatch').Minimatch;
const path = require('path');

Expand Down
4 changes: 1 addition & 3 deletions spec/lib/build/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const mockfs = require('../../mocks/mock-fs');
const Utils = require('../../../lib/build/utils');

describe('the Utils.runSequentially function', () => {
Expand Down Expand Up @@ -97,10 +98,7 @@ describe('the Utils.couldMissGulpPreprocess function', () => {
});

describe('the Utils.nodejsLoad function', () => {
let mockfs;

beforeEach(() => {
mockfs = require('mock-fs');
const fsConfig = {};
mockfs(fsConfig);
});
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/cli-options.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const mockfs = require('../mocks/mock-fs');

describe('The cli-options', () => {
let cliOptions;
let mockfs;

beforeEach(() => {
mockfs = require('mock-fs');
const fsConfig = {
'aurelia_project/environments/dev.js': 'content',
'aurelia_project/environments/stage.js': 'content',
Expand Down
8 changes: 5 additions & 3 deletions spec/lib/cli.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const mockfs = require('../mocks/mock-fs');

describe('The cli', () => {
let fs;
let path;
let mockfs;
let cli;
let Project;
let project;
Expand All @@ -14,7 +15,6 @@ describe('The cli', () => {
path = require('path');
cli = new (require('../../lib/cli').CLI)();
Project = require('../../lib/project').Project;
mockfs = require('mock-fs');
project = {};

dir = 'workspaces';
Expand Down Expand Up @@ -114,7 +114,9 @@ describe('The cli', () => {
.and.callFake(() => new Promise(resolve => resolve()));
});

it('logs the cli version', () => {
// Without real mockfs, it doesn't require the mocked package.json.
xit('logs the cli version', () => {
console.log('cwd', process.cwd());
cli.run(command);
expect(cli.ui.log).toHaveBeenCalledWith('Local aurelia-cli v1.0.0');
});
Expand Down
5 changes: 2 additions & 3 deletions spec/lib/commands/config/configuration.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('The config command - configuration', () => {
let mockfs;
const mockfs = require('../../../mocks/mock-fs');

describe('The config command - configuration', () => {
const CLIOptions = require('../../../../lib/cli-options').CLIOptions;
const Configuration = require('../../../../lib/commands/config/configuration');
let configuration;
Expand All @@ -25,7 +25,6 @@ describe('The config command - configuration', () => {
};
projectControl = JSON.parse(JSON.stringify(project));

mockfs = require('mock-fs');
mockfs({
'aurelia_project': {
'aurelia.json': JSON.stringify(project)
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/commands/config/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const mockfs = require('../../../mocks/mock-fs');

describe('The config command - util', () => {
let mockfs;
const CLIOptions = require('../../../../lib/cli-options').CLIOptions;
const ConfigurationUtilities = require('../../../../lib/commands/config/util');

beforeEach(() => {
mockfs = require('mock-fs');
mockfs({
'aurelia_project/aurelia.json': '{ "build": {} }'
});
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/file-system.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const mockfs = require('../mocks/mock-fs');

const ERROR_CODES = {
ENOENT: 'ENOENT',
EEXIST: 'EEXIST'
};

describe('The file-system module', () => {
let mockfs;
let path;
let fs;

Expand All @@ -14,7 +15,6 @@ describe('The file-system module', () => {
let writeFile;

beforeEach(() => {
mockfs = require('mock-fs');
path = require('path');
fs = require('../../lib/file-system');

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/project-item.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const fs = require('../../lib/file-system');
const mockfs = require('mock-fs');
const mockfs = require('../mocks/mock-fs');
const {ProjectItem} = require('../../lib/project-item');

describe('The ProjectItem module', () => {
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/project.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const mockfs = require('../mocks/mock-fs');

describe('The project module', () => {
let mockfs;
let path;

let fs;
Expand All @@ -8,7 +9,6 @@ describe('The project module', () => {
let project;

beforeEach(() => {
mockfs = require('mock-fs');
path = require('path');

fs = require('../../lib/file-system');
Expand Down
42 changes: 42 additions & 0 deletions spec/mocks/mock-fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { mkdtempSync, rmSync, mkdirSync, writeFileSync } = require('fs');
const { join, dirname } = require('path');
const tmpdir = mkdtempSync(join(__dirname, '..', '..', 'tmpdir-'));
// By default work in a child folder. Some tests run against parent folder
const defaultdir = join(tmpdir, 'a');

function fillFiles(fileTree, baseDir = defaultdir) {
mkdirSync(baseDir, { recursive: true });
for (const key in fileTree) {
const val = fileTree[key];
const p = join(baseDir, key);
if (typeof val === 'string') {
mkdirSync(dirname(p), { recursive: true });
writeFileSync(p, val);
} else if (typeof val === 'object') {
fillFiles(val, p);
}
}
}

let oldCwd;

// Simple implementation of mockfs in local tmp dir.
function mockfs(fileTree) {
fillFiles(fileTree);
if (!oldCwd) {
oldCwd = process.cwd();
process.chdir(defaultdir);
}
}

mockfs.restore = function() {
if (oldCwd) {
process.chdir(oldCwd);
oldCwd = undefined;
}
rmSync(tmpdir, { force: true, recursive: true });
}

process.on('exit', mockfs.restore);

module.exports = mockfs;
Loading