Skip to content

Commit

Permalink
fix: fix cr
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaSummer committed Jan 24, 2024
1 parent 0bf7a42 commit b96c109
Show file tree
Hide file tree
Showing 16 changed files with 372 additions and 246 deletions.
2 changes: 2 additions & 0 deletions .mocharc-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ require:
- intelli-espower-loader
full-trace: true
exit: true
jobs: 1
parallel: false
2 changes: 2 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ require:
- intelli-espower-loader
full-trace: true
exit: true
jobs: 1
parallel: false
1 change: 1 addition & 0 deletions Cargo.lock

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

63 changes: 63 additions & 0 deletions integration/fixtures/utils/pids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';
const execa = require('execa');

class Pids {
constructor(nodeModulesDir) {
this.nodeModulesDir = nodeModulesDir;
}

async getPsSnapshot() {
try {
const { stdout } = await execa.command('ps aux');
return stdout;
} catch (error) {
throw new Error(`Failed to execute 'ps aux': ${error.message}`, { cause: error });
}
}

async getPids() {
const pids = [];

try {
const snapshot = await this.getPsSnapshot();

let overlayPattern;

let nfsPattern;

if (process.platform === 'linux') {
overlayPattern = new RegExp(`overlay.*?${this.nodeModulesDir}`, 'i');
} else {
overlayPattern = new RegExp(`unionfs.*?${this.nodeModulesDir}`, 'i');
nfsPattern = new RegExp(
`/usr/local/bin/go-nfsv4.*?${this.nodeModulesDir}`, 'i'
);
}

for (const line of snapshot.split('\n')) {
if (overlayPattern.test(line)) {
const fields = line.split(/\s+/);
if (fields.length >= 11) {
const pid = parseInt(fields[1], 10) || 0;
pids.push(pid);
}
}
if (process.platform !== 'linux') {
if (nfsPattern.test(line)) {
const fields = line.split(/\s+/);
if (fields.length >= 11) {
const pid = parseInt(fields[1], 10) || 0;
pids.push(pid);
}
}
}
}

return pids;
} catch (error) {
throw new Error(`Failed to get PIDs: ${error.message}`, { cause: error });
}
}
}

exports.Pids = Pids;
135 changes: 42 additions & 93 deletions integration/index.2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ const assert = require('node:assert');
const coffee = require('coffee');
const semver = require('semver');
const execa = require('execa');
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
const { execSync } = require('node:child_process');
const { setTimeout } = require('node:timers/promises');
const rapid = path.join(__dirname, '../node_modules/.bin/rapid');
const { Pids } = require(path.join(__dirname, './fixtures/utils/pids'));
const {
clean,
} = require('@cnpmjs/rapid');
Expand All @@ -18,58 +17,6 @@ const {
forceExitDaemon,
} = require('@cnpmjs/rapid/lib/nydusd/nydusd_api');


class Pids {
constructor(nodeModulesDir) {
this.nodeModulesDir = nodeModulesDir;
}

async getPsSnapshot() {
try {
const { stdout } = await exec('ps aux');
return stdout;
} catch (error) {
throw new Error(`Failed to execute 'ps aux': ${error.message}`);
}
}

async getPids() {
const pids = [];

try {
const snapshot = await this.getPsSnapshot();

const overlayPattern = new RegExp(`overlay.*?${this.nodeModulesDir}`, 'i');
const nfsPattern = new RegExp(
`/usr/local/bin/go-nfsv4.*?${this.nodeModulesDir}`, 'i'
);

snapshot.split('\n').forEach(line => {
if (overlayPattern.test(line)) {
const fields = line.split(/\s+/);
if (fields.length >= 11) {
const pid = parseInt(fields[1], 10) || 0;
pids.push(pid);
}
}

if (nfsPattern.test(line)) {
const fields = line.split(/\s+/);
if (fields.length >= 11) {
const pid = parseInt(fields[1], 10) || 0;
pids.push(pid);
}
}
});

return pids;
} catch (error) {
console.log(error);
throw new Error(`Failed to get PIDs: ${error.message}`);
}
}
}

describe('test/index.v2.test.js', () => {
let cwd;

Expand Down Expand Up @@ -203,44 +150,6 @@ describe('test/index.v2.test.js', () => {
assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14');
});

it('deamon should working', async () => {
cwd = path.join(__dirname, './fixtures/esbuild');
await coffee
.fork(rapid, [
'install',
'--ignore-scripts',
], {
cwd,
})
.debug()
.expect('code', 0)
.end();

const dirs = await fs.readdir(path.join(cwd, 'node_modules'));
assert.strictEqual(dirs.filter(dir => dir.includes('esbuild')).length, 2);
await assert.doesNotReject(fs.stat(path.join(cwd, 'node_modules/esbuild')));
assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14');


const pidsInstance = new Pids(dirs);
let pids = await pidsInstance.getPids();
assert(pids.length > 0);
pids.forEach(pid => {
try {
execSync(`kill -9 ${pid}`);
} catch (err) {
console.error(`Failed to kill process with PID ${pid}. Error: ${err.message}`);
}
});
await new Promise(resolve => {
setTimeout(() => {
resolve();
}, 10000);
});
pids = await pidsInstance.getPids();
assert(pids.length > 0);
});

it('should install optional deps successfully use npminstall', async () => {
cwd = path.join(__dirname, './fixtures/esbuild');
await coffee
Expand Down Expand Up @@ -296,4 +205,44 @@ describe('test/index.v2.test.js', () => {
assert(res.stdout.indexOf('integration/fixtures/esbuild/node_modules') === res.stdout.lastIndexOf('integration/fixtures/esbuild/node_modules'));
});


describe('deamon', async () => {
it('should work', async () => {
cwd = path.join(__dirname, './fixtures/esbuild');
await coffee
.fork(rapid, [
'install',
'--ignore-scripts',
], {
cwd,
})
.debug()
.expect('code', 0)
.end();

const dirs = await fs.readdir(path.join(cwd, 'node_modules'));
assert.strictEqual(dirs.filter(dir => dir.includes('esbuild')).length, 2);
await assert.doesNotReject(fs.stat(path.join(cwd, 'node_modules/esbuild')));
assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14');

const pidsInstance = new Pids('esbuild/node_modules');
let pids = await pidsInstance.getPids();
assert(pids.length > 0);
for (const pid of pids) {
try {
await execa.command(`kill -9 ${pid}`);
} catch (err) {
assert.expected(`Failed to kill process with PID ${pid}. Error: ${err.message}`);
}
}
await new Promise(resolve => {
setTimeout(() => {
resolve();
}, 10000);
});
pids = await pidsInstance.getPids();
assert(pids.length > 0);
});
});

});
12 changes: 6 additions & 6 deletions packages/cli/bin/rapid.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const argv = yargs
type: 'array',
default: [],
})
.option('ignore-deamon', {
.option('nodaemon', {
describe: 'Will not run deamon',
type: 'boolean',
});
Expand All @@ -42,7 +42,7 @@ const argv = yargs
const ignoreScripts = argv['ignore-scripts'];
const mode = argv.by || NpmFsMode.NPM;
const productionMode = argv.production || argv.omit.includes('dev') || process.env.NODE_ENV === 'production';
const ignoreDeamon = argv['ignore-deamon'];
const nodaemon = argv.nodaemon;

const cwd = process.cwd();
const pkgRes = await util.readPkgJSON();
Expand All @@ -62,7 +62,7 @@ const argv = yargs
nydusMode: NYDUS_TYPE.FUSE,
ignoreScripts,
productionMode,
ignoreDeamon,
nodaemon,
});

Alert.success('🚀 Success', [
Expand All @@ -80,15 +80,15 @@ const argv = yargs
describe: 'Clean up the project',
builder: yargs => {
return yargs
.option('ignore-deamon', {
.option('nodaemon', {
describe: 'Will not run deamon',
type: 'boolean',
});
},
handler: async argv => {
const cwd = argv.path || process.cwd();
const ignoreDeamon = argv['ignore-deamon'];
await clean({ nydusMode: NYDUS_TYPE.FUSE, cwd, force: true, ignoreDeamon });
const nodaemon = argv.nodaemon;
await clean({ nydusMode: NYDUS_TYPE.FUSE, cwd, force: true, nodaemon });
console.log('[rapid] clean finished');
},
})
Expand Down
Loading

0 comments on commit b96c109

Please sign in to comment.