Skip to content

Commit 7a835d1

Browse files
Added logging for when creating/updating/removing config file.
Signed-off-by: victor.linroth.sensmetry <victor.linroth@sensmetry.com>
1 parent c64ed8e commit 7a835d1

2 files changed

Lines changed: 108 additions & 55 deletions

File tree

core/src/config/local_fs.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ pub fn add_project_source_to_config<P: AsRef<Utf8Path>, S: AsRef<str>>(
8181
let contents = if config_path.as_ref().is_file() {
8282
wrapfs::read_to_string(&config_path)?
8383
} else {
84+
let creating = "Creating";
85+
let header = crate::style::get_style_config().header;
86+
log::info!(
87+
"{header}{creating:>12}{header:#} configuration file at `{}`",
88+
config_path.as_ref(),
89+
);
8490
String::new()
8591
};
8692
let mut config = DocumentMut::from_str(&contents)?;
@@ -112,6 +118,14 @@ pub fn add_project_source_to_config<P: AsRef<Utf8Path>, S: AsRef<str>>(
112118
projects.push(project);
113119
}
114120

121+
let adding = "Adding";
122+
let header = crate::style::get_style_config().header;
123+
log::info!(
124+
"{header}{adding:>12}{header:#} source for `{}` to configuration file at `{}`",
125+
iri.as_ref(),
126+
config_path.as_ref(),
127+
);
128+
115129
wrapfs::write(&config_path, config.to_string())?;
116130

117131
Ok(())
@@ -144,9 +158,29 @@ pub fn remove_project_source_from_config<P: AsRef<Utf8Path>, S: AsRef<str>>(
144158
})
145159
});
146160

161+
let removing = "Removing";
162+
let header = crate::style::get_style_config().header;
163+
log::info!(
164+
"{header}{removing:>12}{header:#} source for `{}` from configuration file at `{}`",
165+
iri.as_ref(),
166+
config_path.as_ref(),
167+
);
168+
147169
if let Some(index) = remove_index {
148170
projects.remove(index);
149-
wrapfs::write(&config_path, config.to_string())?;
171+
let contents = config.to_string();
172+
173+
if contents.is_empty() {
174+
let removing = "Removing";
175+
log::info!(
176+
"{header}{removing:>12}{header:#} empty configuration file at `{}`",
177+
config_path.as_ref(),
178+
);
179+
wrapfs::remove_file(config_path)?;
180+
} else {
181+
wrapfs::write(config_path, contents)?;
182+
}
183+
150184
return Ok(true);
151185
}
152186

@@ -239,15 +273,7 @@ mod tests {
239273

240274
local_fs::remove_project_source_from_config(&config_path, iri)?;
241275

242-
assert_eq!(
243-
Config {
244-
quiet: None,
245-
verbose: None,
246-
index: None,
247-
projects: vec![]
248-
},
249-
toml::from_str(wrapfs::read_to_string(config_path)?.as_str())?,
250-
);
276+
assert!(!config_path.is_file());
251277

252278
Ok(())
253279
}

sysand/tests/cli_add_remove.rs

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ fn add_and_remove_with_editable() -> Result<(), Box<dyn std::error::Error>> {
7171
out.assert().success();
7272

7373
let config_path = cwd.join("sysand.toml");
74-
std::fs::File::create_new(&config_path)?;
7574

7675
let out = run_sysand_in(
7776
&cwd,
@@ -87,7 +86,11 @@ fn add_and_remove_with_editable() -> Result<(), Box<dyn std::error::Error>> {
8786

8887
out.assert()
8988
.success()
90-
.stderr(predicate::str::contains("Adding usage: `urn:kpar:test`"));
89+
.stderr(predicate::str::contains(format!(
90+
r#"Creating configuration file at `{config_path}`
91+
Adding source for `urn:kpar:test` to configuration file at `{config_path}`
92+
Adding usage: `urn:kpar:test`"#
93+
)));
9194

9295
let info_json = std::fs::read_to_string(cwd.join(".project.json"))?;
9396

@@ -125,10 +128,14 @@ sources = [
125128
Some(config_path.as_str()),
126129
)?;
127130

128-
out.assert().success().stderr(predicate::str::contains(
129-
r#"Removing `urn:kpar:test` from usages
130-
Removed `urn:kpar:test`"#,
131-
));
131+
out.assert()
132+
.success()
133+
.stderr(predicate::str::contains(format!(
134+
r#"Removing source for `urn:kpar:test` from configuration file at `{config_path}`
135+
Removing empty configuration file at `{config_path}`
136+
Removing `urn:kpar:test` from usages
137+
Removed `urn:kpar:test`"#
138+
)));
132139

133140
let info_json = std::fs::read_to_string(cwd.join(".project.json"))?;
134141

@@ -142,9 +149,7 @@ sources = [
142149
"#
143150
);
144151

145-
let config = std::fs::read_to_string(config_path)?;
146-
147-
assert_eq!(config, "");
152+
assert!(!config_path.is_file());
148153

149154
Ok(())
150155
}
@@ -159,7 +164,7 @@ fn add_and_remove_with_local_src() -> Result<(), Box<dyn std::error::Error>> {
159164
out.assert().success();
160165

161166
let config_path = cwd.join("sysand.toml");
162-
std::fs::File::create_new(&config_path)?;
167+
163168
std::fs::create_dir_all(cwd.join("local/test"))?;
164169

165170
let out = run_sysand_in(
@@ -170,7 +175,11 @@ fn add_and_remove_with_local_src() -> Result<(), Box<dyn std::error::Error>> {
170175

171176
out.assert()
172177
.success()
173-
.stderr(predicate::str::contains("Adding usage: `urn:kpar:test`"));
178+
.stderr(predicate::str::contains(format!(
179+
r#"Creating configuration file at `{config_path}`
180+
Adding source for `urn:kpar:test` to configuration file at `{config_path}`
181+
Adding usage: `urn:kpar:test`"#
182+
)));
174183

175184
let info_json = std::fs::read_to_string(cwd.join(".project.json"))?;
176185

@@ -208,10 +217,14 @@ sources = [
208217
Some(config_path.as_str()),
209218
)?;
210219

211-
out.assert().success().stderr(predicate::str::contains(
212-
r#"Removing `urn:kpar:test` from usages
213-
Removed `urn:kpar:test`"#,
214-
));
220+
out.assert()
221+
.success()
222+
.stderr(predicate::str::contains(format!(
223+
r#"Removing source for `urn:kpar:test` from configuration file at `{config_path}`
224+
Removing empty configuration file at `{config_path}`
225+
Removing `urn:kpar:test` from usages
226+
Removed `urn:kpar:test`"#
227+
)));
215228

216229
let info_json = std::fs::read_to_string(cwd.join(".project.json"))?;
217230

@@ -225,9 +238,7 @@ sources = [
225238
"#
226239
);
227240

228-
let config = std::fs::read_to_string(config_path)?;
229-
230-
assert_eq!(config, "");
241+
assert!(!config_path.is_file());
231242

232243
Ok(())
233244
}
@@ -242,7 +253,7 @@ fn add_and_remove_with_local_kpar() -> Result<(), Box<dyn std::error::Error>> {
242253
out.assert().success();
243254

244255
let config_path = cwd.join("sysand.toml");
245-
std::fs::File::create_new(&config_path)?;
256+
246257
std::fs::create_dir(cwd.join("local"))?;
247258
std::fs::File::create_new(cwd.join("local/test.kpar"))?;
248259

@@ -260,7 +271,11 @@ fn add_and_remove_with_local_kpar() -> Result<(), Box<dyn std::error::Error>> {
260271

261272
out.assert()
262273
.success()
263-
.stderr(predicate::str::contains("Adding usage: `urn:kpar:test`"));
274+
.stderr(predicate::str::contains(format!(
275+
r#"Creating configuration file at `{config_path}`
276+
Adding source for `urn:kpar:test` to configuration file at `{config_path}`
277+
Adding usage: `urn:kpar:test`"#
278+
)));
264279

265280
let info_json = std::fs::read_to_string(cwd.join(".project.json"))?;
266281

@@ -298,10 +313,14 @@ sources = [
298313
Some(config_path.as_str()),
299314
)?;
300315

301-
out.assert().success().stderr(predicate::str::contains(
302-
r#"Removing `urn:kpar:test` from usages
303-
Removed `urn:kpar:test`"#,
304-
));
316+
out.assert()
317+
.success()
318+
.stderr(predicate::str::contains(format!(
319+
r#"Removing source for `urn:kpar:test` from configuration file at `{config_path}`
320+
Removing empty configuration file at `{config_path}`
321+
Removing `urn:kpar:test` from usages
322+
Removed `urn:kpar:test`"#
323+
)));
305324

306325
let info_json = std::fs::read_to_string(cwd.join(".project.json"))?;
307326

@@ -315,9 +334,7 @@ sources = [
315334
"#
316335
);
317336

318-
let config = std::fs::read_to_string(config_path)?;
319-
320-
assert_eq!(config, "");
337+
assert!(!config_path.is_file());
321338

322339
Ok(())
323340
}
@@ -332,7 +349,6 @@ fn add_and_remove_with_remote_src() -> Result<(), Box<dyn std::error::Error>> {
332349
out.assert().success();
333350

334351
let config_path = cwd.join("sysand.toml");
335-
std::fs::File::create_new(&config_path)?;
336352

337353
let out = run_sysand_in(
338354
&cwd,
@@ -348,7 +364,11 @@ fn add_and_remove_with_remote_src() -> Result<(), Box<dyn std::error::Error>> {
348364

349365
out.assert()
350366
.success()
351-
.stderr(predicate::str::contains("Adding usage: `urn:kpar:test`"));
367+
.stderr(predicate::str::contains(format!(
368+
r#"Creating configuration file at `{config_path}`
369+
Adding source for `urn:kpar:test` to configuration file at `{config_path}`
370+
Adding usage: `urn:kpar:test`"#
371+
)));
352372

353373
let info_json = std::fs::read_to_string(cwd.join(".project.json"))?;
354374

@@ -386,10 +406,14 @@ sources = [
386406
Some(config_path.as_str()),
387407
)?;
388408

389-
out.assert().success().stderr(predicate::str::contains(
390-
r#"Removing `urn:kpar:test` from usages
391-
Removed `urn:kpar:test`"#,
392-
));
409+
out.assert()
410+
.success()
411+
.stderr(predicate::str::contains(format!(
412+
r#"Removing source for `urn:kpar:test` from configuration file at `{config_path}`
413+
Removing empty configuration file at `{config_path}`
414+
Removing `urn:kpar:test` from usages
415+
Removed `urn:kpar:test`"#
416+
)));
393417

394418
let info_json = std::fs::read_to_string(cwd.join(".project.json"))?;
395419

@@ -403,9 +427,7 @@ sources = [
403427
"#
404428
);
405429

406-
let config = std::fs::read_to_string(config_path)?;
407-
408-
assert_eq!(config, "");
430+
assert!(!config_path.is_file());
409431

410432
Ok(())
411433
}
@@ -420,7 +442,6 @@ fn add_and_remove_with_remote_kpar() -> Result<(), Box<dyn std::error::Error>> {
420442
out.assert().success();
421443

422444
let config_path = cwd.join("sysand.toml");
423-
std::fs::File::create_new(&config_path)?;
424445

425446
let out = run_sysand_in(
426447
&cwd,
@@ -436,7 +457,11 @@ fn add_and_remove_with_remote_kpar() -> Result<(), Box<dyn std::error::Error>> {
436457

437458
out.assert()
438459
.success()
439-
.stderr(predicate::str::contains("Adding usage: `urn:kpar:test`"));
460+
.stderr(predicate::str::contains(format!(
461+
r#"Creating configuration file at `{config_path}`
462+
Adding source for `urn:kpar:test` to configuration file at `{config_path}`
463+
Adding usage: `urn:kpar:test`"#
464+
)));
440465

441466
let info_json = std::fs::read_to_string(cwd.join(".project.json"))?;
442467

@@ -474,10 +499,14 @@ sources = [
474499
Some(config_path.as_str()),
475500
)?;
476501

477-
out.assert().success().stderr(predicate::str::contains(
478-
r#"Removing `urn:kpar:test` from usages
479-
Removed `urn:kpar:test`"#,
480-
));
502+
out.assert()
503+
.success()
504+
.stderr(predicate::str::contains(format!(
505+
r#"Removing source for `urn:kpar:test` from configuration file at `{config_path}`
506+
Removing empty configuration file at `{config_path}`
507+
Removing `urn:kpar:test` from usages
508+
Removed `urn:kpar:test`"#
509+
)));
481510

482511
let info_json = std::fs::read_to_string(cwd.join(".project.json"))?;
483512

@@ -491,9 +520,7 @@ sources = [
491520
"#
492521
);
493522

494-
let config = std::fs::read_to_string(config_path)?;
495-
496-
assert_eq!(config, "");
523+
assert!(!config_path.is_file());
497524

498525
Ok(())
499526
}

0 commit comments

Comments
 (0)