From 8505bd4c42b08304c367fcaa17d970ea5775bfeb Mon Sep 17 00:00:00 2001 From: akitaSummer Date: Fri, 1 Mar 2024 15:03:27 +0800 Subject: [PATCH] feat: update test --- .mocharc-integration.yml | 1 + .mocharc.yml | 1 + .../fixtures/not-exist-lock-file/package.json | 5 ++ integration/fixtures/utils/pids.js | 57 ------------------- integration/index.2.test.js | 28 ++++++--- integration/index.test.js | 10 +++- integration/workspaces.test.js | 12 ++-- packages/cli/lib/nydusd/nydusd_api.js | 15 ++--- packages/cli/test/package_lock.test.js | 14 ++--- packages/cli/test/scripts.test.js | 2 + packages/deamon/src/config.rs | 50 +++++++++++----- 11 files changed, 89 insertions(+), 106 deletions(-) create mode 100644 integration/fixtures/not-exist-lock-file/package.json delete mode 100644 integration/fixtures/utils/pids.js diff --git a/.mocharc-integration.yml b/.mocharc-integration.yml index 067fde9..73a191d 100644 --- a/.mocharc-integration.yml +++ b/.mocharc-integration.yml @@ -10,3 +10,4 @@ require: - intelli-espower-loader full-trace: true exit: true +parallel: false diff --git a/.mocharc.yml b/.mocharc.yml index 4d58b1a..78e043a 100644 --- a/.mocharc.yml +++ b/.mocharc.yml @@ -10,3 +10,4 @@ require: - intelli-espower-loader full-trace: true exit: true +parallel: false \ No newline at end of file diff --git a/integration/fixtures/not-exist-lock-file/package.json b/integration/fixtures/not-exist-lock-file/package.json new file mode 100644 index 0000000..2072244 --- /dev/null +++ b/integration/fixtures/not-exist-lock-file/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "canvas": "2.9.0" + } +} \ No newline at end of file diff --git a/integration/fixtures/utils/pids.js b/integration/fixtures/utils/pids.js deleted file mode 100644 index 4e41408..0000000 --- a/integration/fixtures/utils/pids.js +++ /dev/null @@ -1,57 +0,0 @@ -'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(); - console.log(snapshot); - if (process.platform === 'darwin') { - const overlayPattern = new RegExp(`unionfs.*?${this.nodeModulesDir}`, 'i'); - const 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+/); - console.log(fields); - if (fields.length >= 11) { - const pid = parseInt(fields[1], 10) || 0; - pids.push(pid); - } - } - - if (nfsPattern.test(line)) { - const fields = line.split(/\s+/); - console.log(fields); - 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; diff --git a/integration/index.2.test.js b/integration/index.2.test.js index bdd2346..3919781 100644 --- a/integration/index.2.test.js +++ b/integration/index.2.test.js @@ -3,6 +3,7 @@ const fs = require('node:fs/promises'); const path = require('node:path'); const assert = require('node:assert'); +const os = require('node:os'); const coffee = require('coffee'); const semver = require('semver'); const execa = require('execa'); @@ -20,7 +21,11 @@ describe('test/index.v2.test.js', () => { let cwd; afterEach(async () => { - await clean({ cwd }); + try { + await clean({ cwd, daemon: true }); + } catch (e) { + console.warn('clean error: ', e); + } if (process.platform === 'darwin') { try { await forceExitDaemon(); @@ -28,8 +33,13 @@ describe('test/index.v2.test.js', () => { console.warn('force exit daemon error: %s', err.message); } } else { - await exitDaemon(); + try { + await exitDaemon(); + } catch (err) { + console.warn('exit daemon error: %s', err.message); + } } + }); describe('update', () => { @@ -125,8 +135,10 @@ describe('test/index.v2.test.js', () => { .end(); await assert.doesNotReject(fs.stat(path.join(cwd, 'node_modules/react-jsx-parser/package.json'))); - const { stdout } = await execa.command('mount', { stdio: 'pipe' }); - assert(stdout.indexOf(cwd) > 0); + if (os.type() === 'Darwin') { + const { stdout } = await execa.command('mount', { stdio: 'pipe' }); + assert(stdout.indexOf(cwd) > 0); + } assert(require(path.join(cwd, 'node_modules/react-jsx-parser/package.json')).version === '1.29.0'); }); @@ -205,7 +217,7 @@ describe('test/index.v2.test.js', () => { }); - describe('deamon', async () => { + describe.only('deamon', async () => { it('should work', async () => { cwd = path.join(__dirname, './fixtures/esbuild'); await coffee @@ -223,9 +235,8 @@ describe('test/index.v2.test.js', () => { 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 nodeModulesDir = path.join(cwd, 'node_modules'); - await execa.command(`umount -f ${nodeModulesDir}`); + await execa.command('killall -9 nydusd'); await setTimeoutPromise(20000); assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14'); }); @@ -248,9 +259,8 @@ describe('test/index.v2.test.js', () => { 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 nodeModulesDir = path.join(cwd, 'node_modules'); - await execa.command(`umount -f ${nodeModulesDir}`); + await execa.command('killall -9 nydusd'); await setTimeoutPromise(20000); await assert.rejects(fs.stat(path.join(cwd, 'node_modules', 'esbuild/package.json'))); diff --git a/integration/index.test.js b/integration/index.test.js index 8e220bd..70841ca 100644 --- a/integration/index.test.js +++ b/integration/index.test.js @@ -16,9 +16,13 @@ const { describe('test/index.test.js', () => { let fixture; afterEach(async () => { - await clean({ - cwd: fixture, - }); + try { + await clean({ + cwd: fixture, + }); + } catch (e) { + console.warn('clean error: ', e); + } if (process.platform === 'darwin') { await forceExitDaemon(); } else { diff --git a/integration/workspaces.test.js b/integration/workspaces.test.js index 8bb2fb1..c19ed41 100644 --- a/integration/workspaces.test.js +++ b/integration/workspaces.test.js @@ -17,10 +17,14 @@ describe('test/workspaces.test.js', () => { it('should install lodash successfully', async () => { cwd = path.join(__dirname, './fixtures/workspaces'); - await clean({ - cwd, - force: true, - }); + try { + await clean({ + cwd, + force: true, + }); + } catch (e) { + console.warn('clean error: ', e); + } await install({ nydusMode: 'FUSE', cwd, diff --git a/packages/cli/lib/nydusd/nydusd_api.js b/packages/cli/lib/nydusd/nydusd_api.js index f0a1e97..a95e530 100644 --- a/packages/cli/lib/nydusd/nydusd_api.js +++ b/packages/cli/lib/nydusd/nydusd_api.js @@ -145,12 +145,12 @@ async function checkDaemon() { // 优雅退出 nydusd daemon async function exitDaemon() { try { - await killDeamon(); await urllib.request(`${daemonUrl}/exit`, { method: 'PUT', socketPath, dataType: 'json', }); + await killDeamon(); } catch (e) { // ignore, nydusd quits with error, but it's ok e.message = 'exit nydusd faield: ' + e.message; @@ -170,15 +170,6 @@ async function forceExitDaemon() { e.message = 'umount nydusd mnt failed: ' + e.message; console.warn(e); } - - try { - await killDeamon(); - await execa.command('killall -9 nydusd'); - } catch (e) { - // ignore, nydusd quits with error, but it's ok - e.message = 'exit nydusd failed: ' + e.message; - console.warn(e); - } } async function mount(mountpoint, cwd, bootstrap = '') { @@ -218,6 +209,10 @@ async function mount(mountpoint, cwd, bootstrap = '') { }); debug('mount result: %j', result); + if (os.type() === 'Darwin') { + await new Promise(resolve => setTimeout(resolve, 1000)); + } + return config; } diff --git a/packages/cli/test/package_lock.test.js b/packages/cli/test/package_lock.test.js index b3d5c79..158d6fb 100644 --- a/packages/cli/test/package_lock.test.js +++ b/packages/cli/test/package_lock.test.js @@ -5,8 +5,7 @@ const path = require('node:path'); const fs = require('node:fs/promises'); const mm = require('mm'); const PackageLock = require('../lib/package_lock').PackageLock; -const { install } = require('../lib'); -const httpclient = require('../lib/httpclient'); +const { generatePackageLock } = require('../lib/util'); const nydusd = require('../lib/nydusd'); const downloadDependency = require('../lib/download_dependency'); @@ -68,15 +67,10 @@ describe('test/package_lock.test.js', () => { mm.restore(); }); - it('should run all project installation scripts', async () => { + it('should generate package-lock.json', async () => { fixture = path.join(__dirname, './fixtures/not-exist-lock-file'); - const pkg = require(path.join(fixture, 'package.json')); - await install({ - httpclient, - pkg, - cwd: fixture, - console: global.console, - }); + + await generatePackageLock(fixture); await fs.stat(path.join(fixture, 'package-lock.json')); }); }); diff --git a/packages/cli/test/scripts.test.js b/packages/cli/test/scripts.test.js index d426021..201b9c4 100644 --- a/packages/cli/test/scripts.test.js +++ b/packages/cli/test/scripts.test.js @@ -163,6 +163,8 @@ describe('test/scripts.test.js', () => { mm(process.env, NYDUS_CSI_ROOT_ENV, 'true'); mm(process, 'cwd', () => fixtures); mm(nydusd, 'startNydusFs', async () => { }); + mm(util, 'ensureAccess', async () => { }); + mm(util, 'storePackageLock', async () => { }); mm(downloadDependency, 'download', async () => { return { depsTree: [ 1 ], diff --git a/packages/deamon/src/config.rs b/packages/deamon/src/config.rs index c45bd9d..a41872d 100644 --- a/packages/deamon/src/config.rs +++ b/packages/deamon/src/config.rs @@ -6,10 +6,10 @@ use log::{error, info}; use regex::Regex; use serde::{Deserialize, Serialize}; use serde_json::Error; -use std::{path::PathBuf, process::Command}; use std::os::unix::fs::PermissionsExt; +use std::{path::PathBuf, process::Command}; -use crate::utils::{del_dir_if_exists, get_ps_snapshot, start_command, create_dir_if_not_exists}; +use crate::utils::{create_dir_if_not_exists, del_dir_if_exists, get_ps_snapshot, start_command}; // { // projectName: "", @@ -59,7 +59,6 @@ pub struct NydusdConfig { overlay: Option, } - #[derive(Debug, Deserialize, Serialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub struct NydusdOverlayConfig { @@ -120,28 +119,46 @@ impl NydusdApiMount { let homedir = get_my_home()?; let base = match homedir { - Some(home) => home.join(".rapid/cache/mnt").join(self.mountpoint.clone()).to_string_lossy().to_string(), - None => return Err(anyhow!("Error executing link_node_modules: get home path false")), + Some(home) => home + .join(".rapid/cache/mnt") + .join(self.mountpoint.clone()) + .to_string_lossy() + .to_string(), + None => { + return Err(anyhow!( + "Error executing link_node_modules: get home path false" + )) + } }; let str = format!(r#"ln -s {} {}"#, base, self.node_modules_dir); match start_command(&str) { Ok(output) => { if output.status.success() { - info!("link_node_modules success base {} target {}", base, self.node_modules_dir); + info!( + "link_node_modules success base {} target {}", + base, self.node_modules_dir + ); return Ok(()); } else { return Err(anyhow!( "Error executing link_node_modules, status: {:?}, stdout: {:?}, stderr: {:?}, base {}, target {}", output.status, std::str::from_utf8(&output.stdout)?, - std::str::from_utf8(&output.stderr)?, - base, + std::str::from_utf8(&output.stderr)?, + base, self.node_modules_dir, )); } } - Err(e) => return Err(anyhow!("Error executing link_node_modules: {:?}, base {}, target {}", e, base, self.node_modules_dir)), + Err(e) => { + return Err(anyhow!( + "Error executing link_node_modules: {:?}, base {}, target {}", + e, + base, + self.node_modules_dir + )) + } } } @@ -327,9 +344,13 @@ impl Overlay { create_dir_if_not_exists(self.overlay.clone())?; create_dir_if_not_exists(self.upper.clone())?; create_dir_if_not_exists(self.workdir.clone())?; - std::fs::metadata(&self.upper)?.permissions().set_mode(0o777); - std::fs::metadata(&self.workdir)?.permissions().set_mode(0o777); - + std::fs::metadata(&self.upper)? + .permissions() + .set_mode(0o777); + std::fs::metadata(&self.workdir)? + .permissions() + .set_mode(0o777); + Ok(()) } } @@ -405,7 +426,10 @@ impl ProjectConfig { } pub fn get_node_modules_paths(&self) -> Vec { - self.nydusd_api_mount.iter().map(|c| c.node_modules_dir.clone()).collect() + self.nydusd_api_mount + .iter() + .map(|c| c.node_modules_dir.clone()) + .collect() } pub fn get_pids(&self) -> Result> {