Sitelet https://github.com/krzkaczor/ny/commit/91e75a8773aef41e1a68328bb0e360e99104bfd2
Skip to content

Commit 91e75a8

Browse files
authored
Fix regression that caused running commands to not forward output (#14)
2 parents ecef726 + 0033528 commit 91e75a8

6 files changed

Lines changed: 80 additions & 21 deletions

File tree

‎src/bin/ny.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn main() -> Result<()> {
3737
dev,
3838
workspace_root,
3939
}) => {
40-
add(&executor, &agent, dev, workspace_root, &packages, true)?;
40+
add(&executor, &agent, dev, workspace_root, &packages, false)?;
4141
if check_if_ts_repo(&fs, &cwd) {
4242
install_ts_types(&executor, &fs, &agent, &cwd, &packages, workspace_root)
4343
} else {

‎src/common/commands/add.rs‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub fn add(
77
dev: bool,
88
workspace_root: bool,
99
packages: &[String],
10-
verbose: bool,
10+
silence_stdout: bool,
1111
) -> Result<()> {
1212
let packages_refs: Vec<_> = packages.iter().map(|s| s.as_str()).collect();
1313

@@ -40,7 +40,8 @@ pub fn add(
4040
packages_refs,
4141
),
4242
None,
43-
verbose,
43+
true,
44+
silence_stdout,
4445
),
4546
Agent::Yarn => executor.execute(
4647
"yarn",
@@ -51,7 +52,8 @@ pub fn add(
5152
packages_refs,
5253
),
5354
None,
54-
verbose,
55+
true,
56+
silence_stdout,
5557
),
5658
Agent::Pnpm => executor.execute(
5759
"pnpm",
@@ -62,7 +64,8 @@ pub fn add(
6264
packages_refs,
6365
),
6466
None,
65-
verbose,
67+
true,
68+
silence_stdout,
6669
),
6770
}
6871
}
@@ -82,6 +85,8 @@ mod tests {
8285
"npm",
8386
vec_of_strings!("install", "packageA", "packageB"),
8487
None,
88+
true,
89+
false,
8590
);
8691

8792
let result = add(
@@ -104,6 +109,8 @@ mod tests {
104109
"npm",
105110
vec_of_strings!("install", "packageA", "packageB"),
106111
None,
112+
true,
113+
false,
107114
);
108115

109116
let result = add(
@@ -126,6 +133,8 @@ mod tests {
126133
"yarn",
127134
vec_of_strings!("add", "-D", "packageA", "packageB"),
128135
None,
136+
true,
137+
false,
129138
);
130139

131140
let result = add(
@@ -148,6 +157,8 @@ mod tests {
148157
"yarn",
149158
vec_of_strings!("add", "-D", "-W", "packageA", "packageB"),
150159
None,
160+
true,
161+
false,
151162
);
152163

153164
let result = add(
@@ -170,6 +181,8 @@ mod tests {
170181
"pnpm",
171182
vec_of_strings!("add", "-D", "packageA", "packageB"),
172183
None,
184+
true,
185+
false,
173186
);
174187

175188
let result = add(
@@ -192,6 +205,8 @@ mod tests {
192205
"pnpm",
193206
vec_of_strings!("add", "-D", "-w", "packageA", "packageB"),
194207
None,
208+
true,
209+
false,
195210
);
196211

197212
let result = add(

‎src/common/commands/install.rs‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::{agent::Agent, execute::Executor};
44

55
pub fn install(executor: &dyn Executor, agent: &Agent) -> Result<()> {
66
match agent {
7-
Agent::Npm => executor.execute("npm", &["install"], None, true),
8-
Agent::Yarn => executor.execute("yarn", &["install"], None, true),
9-
Agent::Pnpm => executor.execute("pnpm", &["install"], None, true),
7+
Agent::Npm => executor.execute("npm", &["install"], None, true, false),
8+
Agent::Yarn => executor.execute("yarn", &["install"], None, true, false),
9+
Agent::Pnpm => executor.execute("pnpm", &["install"], None, true, false),
1010
}
1111
}
1212

@@ -20,7 +20,14 @@ mod tests {
2020
#[test]
2121
fn test_install_npm() {
2222
let mut mock_executor = MockExecutor::new();
23-
expect_execute_once(&mut mock_executor, "npm", vec_of_strings!("install"), None);
23+
expect_execute_once(
24+
&mut mock_executor,
25+
"npm",
26+
vec_of_strings!("install"),
27+
None,
28+
true,
29+
false,
30+
);
2431

2532
let result = install(&mock_executor, &Agent::Npm);
2633

@@ -30,7 +37,14 @@ mod tests {
3037
#[test]
3138
fn test_install_yarn() {
3239
let mut mock_executor = MockExecutor::new();
33-
expect_execute_once(&mut mock_executor, "yarn", vec_of_strings!("install"), None);
40+
expect_execute_once(
41+
&mut mock_executor,
42+
"yarn",
43+
vec_of_strings!("install"),
44+
None,
45+
true,
46+
false,
47+
);
3448

3549
let result = install(&mock_executor, &Agent::Yarn);
3650

@@ -40,7 +54,14 @@ mod tests {
4054
#[test]
4155
fn test_install_pnpm() {
4256
let mut mock_executor = MockExecutor::new();
43-
expect_execute_once(&mut mock_executor, "pnpm", vec_of_strings!("install"), None);
57+
expect_execute_once(
58+
&mut mock_executor,
59+
"pnpm",
60+
vec_of_strings!("install"),
61+
None,
62+
true,
63+
false,
64+
);
4465

4566
let result = install(&mock_executor, &Agent::Pnpm);
4667

‎src/common/commands/install_ts_types.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn install_ts_types(
4747
true,
4848
workspace_root,
4949
&packages_to_install,
50-
false,
50+
true, // silence output of PM's add command. The command itself is still printed.
5151
)?;
5252
}
5353

@@ -224,6 +224,8 @@ mod tests {
224224
"npm",
225225
vec_of_strings!["install", "--save-dev", "@types/package-a"],
226226
None,
227+
true,
228+
true,
227229
);
228230
let agent = Agent::Npm;
229231

‎src/common/commands/run.rs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ pub fn run(
2727

2828
println!("{}", format!("$ {}", script).dimmed());
2929

30-
executor.execute("sh", &["-c", &script], Some(bin_path), false)
30+
executor.execute(
31+
"sh",
32+
&["-c", &script],
33+
Some(bin_path),
34+
false, // do not print command as it's quite odd to see "sh -c <script>"
35+
false, // do not silence output
36+
)
3137
} else {
3238
let program = task;
3339
println!("{}", format!("$ {}", program).dimmed());
@@ -36,7 +42,8 @@ pub fn run(
3642
program,
3743
extra_args.unwrap_or_default(),
3844
Some(bin_path),
39-
false,
45+
false, // do not print command as it's quite odd to see "sh -c <script>"
46+
false, // do not silence output
4047
)
4148
}
4249
}
@@ -115,6 +122,8 @@ mod tests {
115122
"sh",
116123
vec_of_strings!("-c", r#"mocha "*.ts" --no-timeout --bail"#),
117124
Some("/project/node_modules/.bin:/node_modules/.bin:".to_string()),
125+
false,
126+
false,
118127
);
119128
let mut mock_fs = MockFilesystem::new();
120129
expect_file(
@@ -142,6 +151,8 @@ mod tests {
142151
"mocha",
143152
vec_of_strings!("--help"),
144153
Some("/project/node_modules/.bin:/node_modules/.bin:".to_string()),
154+
false,
155+
false,
145156
);
146157
let mut mock_fs = MockFilesystem::new();
147158
expect_file(

‎src/common/execute.rs‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ pub trait Executor {
1111
program: &str,
1212
args: &[&'a str],
1313
extra_path: Option<String>,
14-
verbose: bool,
14+
verbose: bool, // print out the command being executed
15+
silence_stdout: bool, // silence "regular" output, still print out errors
1516
) -> Result<()>;
1617
}
1718

@@ -23,6 +24,7 @@ impl Executor for RealExecutor {
2324
args: &[&str],
2425
extra_path: Option<String>,
2526
verbose: bool,
27+
silence_stdout: bool,
2628
) -> Result<()> {
2729
if verbose {
2830
println!("{}", format!("$ {} {}", program, args.join(" ")).dimmed());
@@ -35,7 +37,7 @@ impl Executor for RealExecutor {
3537
let current_path = std::env::var("PATH").unwrap_or_else(|_| "".to_string());
3638
cmd_builder.env("PATH", extra_env + ":" + &current_path);
3739
}
38-
if !verbose {
40+
if silence_stdout {
3941
cmd_builder.stdout(std::process::Stdio::null());
4042
}
4143

@@ -60,15 +62,23 @@ pub fn expect_execute_once(
6062
program: &str,
6163
args: Vec<String>,
6264
extra_path: Option<String>,
65+
verbose: bool,
66+
silence_stdout: bool,
6367
) {
6468
let program = program.to_owned();
6569
mock_executor
6670
.expect_execute()
6771
.times(1)
68-
.withf(move |_program, _args, _extra_path, _verbose| {
69-
_program == program && _args == args && _extra_path == &extra_path
70-
})
71-
.returning(|_, _, _, _| Ok(()));
72+
.withf(
73+
move |_program, _args, _extra_path, _verbose, _silence_stdout| {
74+
_program == program
75+
&& _args == args
76+
&& _extra_path == &extra_path
77+
&& _verbose == &verbose
78+
&& _silence_stdout == &silence_stdout
79+
},
80+
)
81+
.returning(|_, _, _, _, _| Ok(()));
7282
}
7383

7484
#[cfg(test)]
@@ -78,7 +88,7 @@ mod tests {
7888
#[test]
7989
fn test_execute() {
8090
let executor = RealExecutor {};
81-
let result = executor.execute("sh", &["-c", "true"], None, false);
91+
let result = executor.execute("sh", &["-c", "true"], None, false, false);
8292
assert!(result.is_ok());
8393
}
8494
}

0 commit comments

Comments
 (0)