Skip to content

Commit

Permalink
chore(git): merge from main
Browse files Browse the repository at this point in the history
  • Loading branch information
KSXGitHub committed Nov 15, 2023
2 parents 8b3e8e9 + 078359e commit 3b2696a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
16 changes: 10 additions & 6 deletions tasks/integrated-benchmark/src/cli_args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::fixtures::LOCKFILE;
use clap::{Args, Parser, ValueEnum};
use pipe_trait::Pipe;
use std::{path::PathBuf, process::Command};

#[derive(Debug, Parser)]
Expand All @@ -20,9 +20,9 @@ pub struct CliArgs {
#[clap(long, short = 'R', default_value = ".")]
pub repository: PathBuf,

/// Override default `package.json`.
#[clap(long, short)]
pub package_json: Option<PathBuf>,
/// Override default `package.json` and `pnpm-lock.yaml` by specifying the directory containing them.
#[clap(long, short = 'D')]
pub fixture_dir: Option<PathBuf>,

/// Flags to pass to `hyperfine`.
#[clap(flatten)]
Expand Down Expand Up @@ -67,10 +67,14 @@ impl BenchmarkScenario {
}

/// Whether to use a lockfile.
pub fn lockfile(self) -> Option<&'static str> {
pub fn lockfile<Text, LoadLockfile>(self, load_lockfile: LoadLockfile) -> Option<String>
where
Text: Into<String>,
LoadLockfile: FnOnce() -> Text,
{
match self {
BenchmarkScenario::CleanInstall => None,
BenchmarkScenario::FrozenLockfile => Some(LOCKFILE),
BenchmarkScenario::FrozenLockfile => load_lockfile().into().pipe(Some),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tasks/integrated-benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async fn main() {
registry,
verdaccio,
repository,
package_json,
fixture_dir,
hyperfine_options,
work_env,
with_pnpm,
Expand Down Expand Up @@ -52,7 +52,7 @@ async fn main() {
repository,
scenario,
hyperfine_options,
package_json,
fixture_dir,
}
.run();
drop(verdaccio); // terminate verdaccio if exists
Expand Down
30 changes: 20 additions & 10 deletions tasks/integrated-benchmark/src/work_env.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{
cli_args::{BenchmarkScenario, HyperfineOptions},
fixtures::PACKAGE_JSON,
fixtures::{LOCKFILE, PACKAGE_JSON},
};
use itertools::Itertools;
use os_display::Quotable;
use pacquet_fs::make_file_executable;
use pipe_trait::Pipe;
use std::{
borrow::Cow,
fmt,
fs::{self, File},
io::Write,
Expand All @@ -24,7 +25,7 @@ pub struct WorkEnv {
pub repository: PathBuf,
pub scenario: BenchmarkScenario,
pub hyperfine_options: HyperfineOptions,
pub package_json: Option<PathBuf>,
pub fixture_dir: Option<PathBuf>,
}

impl WorkEnv {
Expand Down Expand Up @@ -99,10 +100,10 @@ impl WorkEnv {
let dir = self.bench_dir(id);
let for_pnpm = matches!(id, BenchId::Static(_));
fs::create_dir_all(&dir).expect("create directory for the revision");
create_package_json(&dir, self.package_json.as_deref());
create_package_json(&dir, self.fixture_dir.as_deref());
create_install_script(&dir, self.scenario, for_pnpm);
create_npmrc(&dir, self.registry(), self.scenario);
may_create_lockfile(&dir, self.scenario);
may_create_lockfile(&dir, self.scenario, self.fixture_dir.as_deref());
}

eprintln!("Populating proxy registry cache...");
Expand Down Expand Up @@ -203,9 +204,10 @@ impl WorkEnv {
}
}

fn create_package_json(dir: &Path, src: Option<&Path>) {
let dst = dir.join("package.json");
if let Some(src) = src {
fn create_package_json(dst_dir: &Path, src_dir: Option<&Path>) {
let dst = dst_dir.join("package.json");
if let Some(src_dir) = src_dir {
let src = src_dir.join("package.json");
assert!(src.is_file(), "{src:?} must be a file");
assert_ne!(src, dst);
fs::copy(src, dst).expect("copy package.json for the revision");
Expand All @@ -227,9 +229,17 @@ fn create_npmrc(dir: &Path, registry: &str, scenario: BenchmarkScenario) {
writeln!(file, "{}", scenario.npmrc_lockfile_setting()).unwrap();
}

fn may_create_lockfile(dir: &Path, scenario: BenchmarkScenario) {
if let Some(lockfile) = scenario.lockfile() {
let path = dir.join("pnpm-lock.yaml");
fn may_create_lockfile(dst_dir: &Path, scenario: BenchmarkScenario, src_dir: Option<&Path>) {
let load_lockfile = || -> Cow<'_, str> {
let Some(src_dir) = src_dir else { return Cow::Borrowed(LOCKFILE) };
src_dir
.join("pnpm-lock.yaml")
.pipe(fs::read_to_string)
.expect("read fixture lockfile")
.pipe(Cow::Owned)
};
if let Some(lockfile) = scenario.lockfile(load_lockfile) {
let path = dst_dir.join("pnpm-lock.yaml");
fs::write(path, lockfile).expect("write pnpm-lock.yaml for the revision");
}
}
Expand Down

0 comments on commit 3b2696a

Please sign in to comment.