Skip to content

Commit 4a5c027

Browse files
authored
Merge pull request #2529 from costajohnt/feat/no-format-convert
Add --no-format flag to nickel convert
2 parents f49e2c9 + a61503b commit 4a5c027

15 files changed

+91
-6
lines changed

cli/src/convert.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ pub struct ConvertCommand {
3030
/// Output file. Standard output by default
3131
#[arg(short, long)]
3232
pub output: Option<PathBuf>,
33+
34+
/// Skip formatting the output. Useful for batch conversions or when
35+
/// formatted output isn't needed.
36+
#[arg(long = "no-formatting")]
37+
pub no_formatting: bool,
3338
}
3439

3540
impl ConvertCommand {
@@ -114,14 +119,18 @@ impl ConvertCommand {
114119
let ast = ast.map_err(|error| Error::Program { files, error })?;
115120

116121
let nickel_string = {
117-
let mut output = Vec::new();
118122
let unformatted = ast.to_string();
119-
// In principle formatting should be infallible here, but if it fails somehow the
120-
// unformatted string will be returned.
121-
if nickel_lang_core::format::format(unformatted.as_bytes(), &mut output).is_ok() {
122-
String::from_utf8(output).unwrap_or(unformatted)
123-
} else {
123+
if self.no_formatting {
124124
unformatted
125+
} else {
126+
let mut output = Vec::new();
127+
// In principle formatting should be infallible here, but if it fails somehow the
128+
// unformatted string will be returned.
129+
if nickel_lang_core::format::format(unformatted.as_bytes(), &mut output).is_ok() {
130+
String::from_utf8(output).unwrap_or(unformatted)
131+
} else {
132+
unformatted
133+
}
125134
}
126135
};
127136
self.write(&nickel_string)?;

cli/tests/snapshot/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ fn check_conversion_snapshots(path: &str) {
104104
assert_snapshot_filtered!(file.prefixed_test_name("convert_stderr"), err);
105105
}
106106

107+
#[test_resources("cli/tests/snapshot/inputs/**/convert/*")]
108+
fn check_conversion_no_format_snapshots(path: &str) {
109+
let file = TestFile::from_project_path(path);
110+
let invocation = NickelInvocation::new()
111+
.file(&file)
112+
.args(["convert", "--no-formatting"]);
113+
114+
let (out, err) = invocation.snapshot();
115+
assert_snapshot_filtered!(file.prefixed_test_name("convert_no_format_stdout"), out);
116+
assert_snapshot_filtered!(file.prefixed_test_name("convert_no_format_stderr"), err);
117+
}
118+
107119
struct TestFile {
108120
path_buf: PathBuf,
109121
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: cli/tests/snapshot/main.rs
3+
expression: err
4+
---
5+
error: could not determine format of input file `[INPUTS_PATH]/convert/json.foo`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: cli/tests/snapshot/main.rs
3+
expression: err
4+
---
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: cli/tests/snapshot/main.rs
3+
expression: err
4+
---
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: cli/tests/snapshot/main.rs
3+
expression: err
4+
---
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: cli/tests/snapshot/main.rs
3+
expression: err
4+
---
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: cli/tests/snapshot/main.rs
3+
expression: err
4+
---
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: cli/tests/snapshot/main.rs
3+
expression: out
4+
---
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: cli/tests/snapshot/main.rs
3+
expression: out
4+
---
5+
{ foo = [ 1, 2 ], bar = {} }

0 commit comments

Comments
 (0)