Skip to content

Commit 955a305

Browse files
Update tests
1 parent 8f4fa39 commit 955a305

16 files changed

+88
-56
lines changed

crates/icp-cli/src/logging.rs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ use tracing_subscriber::layer::Context;
99
use tracing_subscriber::registry::LookupSpan;
1010
use tracing_subscriber::{Layer, fmt::format};
1111

12+
fn should_color() -> bool {
13+
std::env::var_os("NO_COLOR").is_none()
14+
}
15+
1216
// Debug layer (used with --debug)
1317

1418
type DebugLayer<S> = Filtered<
@@ -29,7 +33,17 @@ pub(crate) fn debug_layer<S: Subscriber + for<'a> LookupSpan<'a>>() -> DebugLaye
2933

3034
// User-facing layer (always active, info/warn/error only)
3135

32-
pub(crate) struct UserLayer;
36+
pub(crate) struct UserLayer {
37+
color: bool,
38+
}
39+
40+
impl UserLayer {
41+
pub(crate) fn new() -> Self {
42+
Self {
43+
color: should_color(),
44+
}
45+
}
46+
}
3347

3448
impl<S: Subscriber> Layer<S> for UserLayer {
3549
fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
@@ -47,19 +61,33 @@ impl<S: Subscriber> Layer<S> for UserLayer {
4761
let stderr = io::stderr();
4862
let mut handle = stderr.lock();
4963

50-
const ERROR_STYLE: Style = AnsiColor::BrightRed.on_default();
51-
const WARN_STYLE: Style = AnsiColor::Yellow.on_default();
52-
const RESET: anstyle::Reset = anstyle::Reset;
53-
54-
match level {
55-
Level::ERROR => {
56-
let _ = writeln!(handle, "{ERROR_STYLE}ERR {RESET}{msg}");
57-
}
58-
Level::WARN => {
59-
let _ = writeln!(handle, "{WARN_STYLE}WARN {RESET}{msg}");
64+
if self.color {
65+
const ERROR_STYLE: Style = AnsiColor::BrightRed.on_default();
66+
const WARN_STYLE: Style = AnsiColor::Yellow.on_default();
67+
const RESET: anstyle::Reset = anstyle::Reset;
68+
69+
match level {
70+
Level::ERROR => {
71+
let _ = writeln!(handle, "{ERROR_STYLE}ERR {RESET}{msg}");
72+
}
73+
Level::WARN => {
74+
let _ = writeln!(handle, "{WARN_STYLE}WARN {RESET}{msg}");
75+
}
76+
_ => {
77+
let _ = writeln!(handle, "{msg}");
78+
}
6079
}
61-
_ => {
62-
let _ = writeln!(handle, "{msg}");
80+
} else {
81+
match level {
82+
Level::ERROR => {
83+
let _ = writeln!(handle, "ERR {msg}");
84+
}
85+
Level::WARN => {
86+
let _ = writeln!(handle, "WARN {msg}");
87+
}
88+
_ => {
89+
let _ = writeln!(handle, "{msg}");
90+
}
6391
}
6492
}
6593
}

crates/icp-cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async fn main() -> Result<(), Error> {
119119
let debug = cli.debug;
120120
let reg = Registry::default()
121121
.with(debug.then(debug_layer))
122-
.with((!debug).then_some(UserLayer));
122+
.with((!debug).then(UserLayer::new));
123123
set_global_default(reg)?;
124124

125125
// Execute the command within a span that includes version and SHA context

crates/icp-cli/src/operations/binding_env_vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub(crate) async fn set_binding_env_vars_many(
161161
" ----- Failed to update environment variables for canister '{}': {} -----",
162162
failure.canister_name, failure.canister_id,
163163
);
164-
error!("Error: '{}'", failure.error);
164+
error!("'{}'", failure.error);
165165
}
166166

167167
return SetBindingEnvVarsManySnafu {

crates/icp-cli/src/operations/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub(crate) async fn build_many_with_progress_bar(
153153
" ----- Failed to build canister '{}' -----",
154154
failure.canister_name,
155155
);
156-
error!("Error: '{}'", failure.error);
156+
error!("'{}'", failure.error);
157157
for line in &failure.progress_output {
158158
error!("{line}");
159159
}

crates/icp-cli/src/operations/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ pub(crate) async fn install_many(
270270
" ----- Failed to install canister '{}': {} -----",
271271
failure.canister_name, failure.canister_id,
272272
);
273-
error!("Error: '{}'", failure.error);
273+
error!("'{}'", failure.error);
274274
}
275275

276276
return InstallManySnafu {

crates/icp-cli/src/operations/settings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ pub(crate) async fn sync_settings_many(
240240
" ----- Failed to update settings for canister '{}': {} -----",
241241
failure.canister_name, failure.canister_id,
242242
);
243-
error!("Error: '{}'", failure.error);
243+
error!("'{}'", failure.error);
244244
}
245245

246246
return SyncSettingsManySnafu {

crates/icp-cli/src/operations/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub(crate) async fn sync_many(
125125
" ----- Failed to sync canister '{}': {} -----",
126126
failure.canister_name, failure.canister_id,
127127
);
128-
error!("Error: '{}'", failure.error);
128+
error!("'{}'", failure.error);
129129
for line in &failure.progress_output {
130130
error!("{line}");
131131
}

crates/icp-cli/tests/build_adapter_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn build_adapter_pre_built_path_invalid_checksum() {
8282
.args(["build", "my-canister"])
8383
.assert()
8484
.failure()
85-
.stdout(
85+
.stderr(
8686
contains("checksum mismatch")
8787
.and(contains("expected: invalid"))
8888
.and(contains(format!("actual: {actual}"))),
@@ -216,7 +216,7 @@ fn build_adapter_pre_built_url_invalid_checksum() {
216216
.args(["build", "my-canister"])
217217
.assert()
218218
.failure()
219-
.stdout(
219+
.stderr(
220220
contains("checksum mismatch")
221221
.and(contains("expected: invalid"))
222222
.and(contains(format!("actual: {actual}"))),

crates/icp-cli/tests/build_tests.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,14 @@ fn build_adapter_display_failing_build_output() {
133133

134134
ctx.icp()
135135
.current_dir(project_dir)
136+
.env("NO_COLOR", "1")
136137
.args(["build", "my-canister"])
137138
.assert()
138139
.failure()
139-
.stdout(contains(expected_output))
140-
.stdout(contains("hide this").not())
141-
.stdout(contains("success 1").not())
142-
.stdout(contains("success 2").not());
140+
.stderr(contains(expected_output))
141+
.stderr(contains("hide this").not())
142+
.stderr(contains("success 1").not())
143+
.stderr(contains("success 2").not());
143144
}
144145

145146
#[test]
@@ -181,12 +182,13 @@ fn build_adapter_display_failing_middle_step_output() {
181182

182183
ctx.icp()
183184
.current_dir(project_dir)
185+
.env("NO_COLOR", "1")
184186
.args(["build", "my-canister"])
185187
.assert()
186188
.failure()
187-
.stdout(contains(expected_output))
188-
.stdout(contains("step 1 ok").not())
189-
.stdout(contains("step 3 should not run").not());
189+
.stderr(contains(expected_output))
190+
.stderr(contains("step 1 ok").not())
191+
.stderr(contains("step 3 should not run").not());
190192
}
191193

192194
#[test]
@@ -227,10 +229,11 @@ fn build_adapter_display_failing_prebuilt_output() {
227229

228230
ctx.icp()
229231
.current_dir(project_dir)
232+
.env("NO_COLOR", "1")
230233
.args(["build", "my-canister"])
231234
.assert()
232235
.failure()
233-
.stdout(contains(expected_output));
236+
.stderr(contains(expected_output));
234237
}
235238

236239
#[test]
@@ -270,10 +273,11 @@ fn build_adapter_display_failing_build_output_no_output() {
270273

271274
ctx.icp()
272275
.current_dir(project_dir)
276+
.env("NO_COLOR", "1")
273277
.args(["build", "my-canister"])
274278
.assert()
275279
.failure()
276-
.stdout(contains(expected_output));
280+
.stderr(contains(expected_output));
277281
}
278282

279283
#[test]
@@ -359,10 +363,11 @@ fn build_adapter_display_script_multiple_commands_output() {
359363

360364
ctx.icp()
361365
.current_dir(project_dir)
366+
.env("NO_COLOR", "1")
362367
.args(["build", "my-canister"])
363368
.assert()
364369
.failure()
365-
.stdout(contains(expected_output));
370+
.stderr(contains(expected_output));
366371
}
367372

368373
#[test]

crates/icp-cli/tests/canister_settings_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ async fn canister_settings_sync() {
11071107
])
11081108
.assert()
11091109
.success()
1110-
.stdout(contains("Wasm memory limit is already set in icp.yaml"));
1110+
.stderr(contains("Wasm memory limit is already set in icp.yaml"));
11111111
confirm_wasm_memory_limit(&ctx, &project_dir, "5_368_709_120");
11121112
sync(&ctx, &project_dir);
11131113
confirm_wasm_memory_limit(&ctx, &project_dir, "4_000_000_000");

0 commit comments

Comments
 (0)