-
Notifications
You must be signed in to change notification settings - Fork 38
Enable fixes for W20 repeated keys - group them into attribute sets #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,47 @@ | ||
| use std::{io::Write, process::Command}; | ||
| use std::{fs, io::Write, process::Command}; | ||
|
|
||
| use tempfile::NamedTempFile; | ||
|
|
||
| pub fn test_cli(expression: &str, args: &[&str]) -> anyhow::Result<String> { | ||
| fn write_fixture(expression: &str) -> anyhow::Result<NamedTempFile> { | ||
| let mut fixture = NamedTempFile::with_suffix(".nix")?; | ||
| fixture.write_all(expression.as_bytes())?; | ||
| fixture.write_all(b"\n")?; // otherwise diff says there's no newline at end of file | ||
|
|
||
| Ok(fixture) | ||
| } | ||
|
|
||
| fn run_cli(path: &std::path::Path, args: &[&str]) -> anyhow::Result<String> { | ||
| let output = Command::new("cargo") | ||
| .arg("run") | ||
| .arg("--") | ||
| .args(args) | ||
| .arg(fixture.path()) | ||
| .arg(path) | ||
| .output()?; | ||
|
|
||
| let stdout = strip_ansi_escapes::strip(output.stdout)?; | ||
| let stdout = String::from_utf8(stdout)?; | ||
| let stdout = stdout.replace(fixture.path().to_str().unwrap(), "<temp_file_path>"); | ||
| let stdout = stdout.replace(path.to_str().unwrap(), "<temp_file_path>"); | ||
|
|
||
| Ok(stdout) | ||
| } | ||
|
|
||
| pub fn test_cli(expression: &str, args: &[&str]) -> anyhow::Result<String> { | ||
| let fixture = write_fixture(expression)?; | ||
| run_cli(fixture.path(), args) | ||
| } | ||
|
|
||
| #[allow(dead_code)] | ||
| pub fn apply_and_check( | ||
| expression: &str, | ||
| fix_args: &[&str], | ||
| check_args: &[&str], | ||
| ) -> anyhow::Result<(String, String, String)> { | ||
| let fixture = write_fixture(expression)?; | ||
| let path = fixture.path(); | ||
|
|
||
| let fix_stdout = run_cli(path, fix_args)?; | ||
| let contents = fs::read_to_string(path)?; | ||
| let check_stdout = run_cli(path, check_args)?; | ||
|
|
||
| Ok((fix_stdout, contents, check_stdout)) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,18 @@ | ||
| --- | ||
| source: bin/tests/repeated_keys.rs | ||
| assertion_line: 7 | ||
| expression: "\"{\\n foo.bar = 1;\\n foo.bar.\\\"hello\\\" = 1;\\n foo.again = 1;\\n}\\n\"" | ||
| --- | ||
|
|
||
| --- <temp_file_path> | ||
| +++ <temp_file_path> [fixed] | ||
| @@ -1,6 +1,8 @@ | ||
| { | ||
| - foo.bar = 1; | ||
| - foo.bar."hello" = 1; | ||
|
Comment on lines
+10
to
+11
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, shouldn't this fail to parse?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Nix itself this seems to be a parsing error. Should we even have such a test expression? It seems that rnix accepts this. Perhaps this is another reason to have a HIR.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I have corrected this in
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, wait, I thought that this was my fork but it's not!
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the parser should not detect invalid attr set formations, this should happen at a later stage. |
||
| - foo.again = 1; | ||
| + foo = { | ||
| + bar = 1; | ||
| + bar."hello" = 1; | ||
| + again = 1; | ||
| + }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --- | ||
| source: bin/tests/repeated_keys.rs | ||
| assertion_line: 7 | ||
| expression: "\"{\\n hardware.bluetooth = {\\n enable = true;\\n };\\n networking.hostName = \\\"nixbox\\\";\\n hardware.nvidia-container-toolkit.enable = true;\\n hardware.nvidia = {\\n modesetting.enable = true;\\n };\\n}\\n\"" | ||
| --- | ||
| --- <temp_file_path> | ||
| +++ <temp_file_path> [fixed] | ||
| @@ -1,11 +1,13 @@ | ||
| { | ||
| - hardware.bluetooth = { | ||
| - enable = true; | ||
| + hardware = { | ||
| + bluetooth = { | ||
| + enable = true; | ||
| + }; | ||
| + nvidia-container-toolkit.enable = true; | ||
| + nvidia = { | ||
| + modesetting.enable = true; | ||
| + }; | ||
| }; | ||
| networking.hostName = "nixbox"; | ||
| - hardware.nvidia-container-toolkit.enable = true; | ||
| - hardware.nvidia = { | ||
| - modesetting.enable = true; | ||
| - }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,24 @@ | ||
| --- | ||
| source: bin/tests/repeated_keys.rs | ||
| assertion_line: 7 | ||
| expression: "\"{\\n foo.baz.bar1 = 1;\\n foo.baz.bar2 = 2;\\n foo.baz.bar3 = 3;\\n foo.baz.bar4 = 4;\\n foo.baz.bar5 = 5;\\n}\\n\"" | ||
| --- | ||
|
|
||
| --- <temp_file_path> | ||
| +++ <temp_file_path> [fixed] | ||
| @@ -1,8 +1,12 @@ | ||
| { | ||
| - foo.baz.bar1 = 1; | ||
| - foo.baz.bar2 = 2; | ||
| - foo.baz.bar3 = 3; | ||
| - foo.baz.bar4 = 4; | ||
| - foo.baz.bar5 = 5; | ||
| + foo = { | ||
| + baz = { | ||
| + bar1 = 1; | ||
| + bar2 = 2; | ||
| + bar3 = 3; | ||
| + bar4 = 4; | ||
| + bar5 = 5; | ||
| + }; | ||
| + }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --- | ||
| source: bin/tests/repeated_keys.rs | ||
| assertion_line: 7 | ||
| expression: "\"{\\n hardware.bluetooth = {\\n enable = true;\\n };\\n networking.hostName = \\\"nixbox\\\";\\n hardware.nvidia-container-toolkit.enable = true;\\n hardware.nvidia = {\\n modesetting.enable = true;\\n };\\n}\\n\"" | ||
| --- | ||
| [W20] Warning: Avoid repeated keys in attribute sets | ||
| ╭─[<temp_file_path>:2:3] | ||
| │ | ||
| 2 │ hardware.bluetooth = { | ||
| · ─────────┬──────── | ||
| · ╰────────── The key hardware is first assigned here ... | ||
| 6 │ hardware.nvidia-container-toolkit.enable = true; | ||
| · ────────────────────┬─────────────────── | ||
| · ╰───────────────────── ... repeated here ... | ||
| 7 │ hardware.nvidia = { | ||
| · ───────┬─────── | ||
| · ╰───────── ... and here. Try hardware = { bluetooth=...; nvidia-container-toolkit.enable=...; nvidia=...; } instead. | ||
| ───╯ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind explaining in English what the problem is and what the strategy behind this solution is?