Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions bin/tests/_utils.rs
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))
}
52 changes: 52 additions & 0 deletions bin/tests/repeated_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,57 @@ generate_tests! {
foo.baz.bar5 = 5;
}
"},

// non-contiguous entries should still be grouped into a fix
indoc! {"
{
hardware.bluetooth = {
enable = true;
};
networking.hostName = \"nixbox\";
hardware.nvidia-container-toolkit.enable = true;
hardware.nvidia = {
modesetting.enable = true;
};
}
"},
],
}

#[test]
fn repeated_keys_fix_removes_nested_repeated_key_warnings() {
let expression = indoc! {r#"
{
services.pcscd.enable = true;
services.xserver.xkb = {
layout = "us";
variant = "";
};
services.swapspace.enable = true;
services.xserver.videoDrivers = [ "nvidia" ];
services.xserver.enable = true;

environment.plasma6.excludePackages = [ ];
environment.etc."environment.d/desktop-environment.conf".text = ''
DESKTOP_SESSION=plasma
'';
environment.variables.KWIN_DRM_PREFER_COLOR_DEPTH = "24";

programs.dconf.enable = true;
programs.nix-ld.enable = true;
programs.nix-ld.libraries = [ ];

security.rtkit.enable = true;
security.polkit.enable = true;
security.sudo.package = pkgs.sudo;
}
"#};

let (_, fixed, check_output) =
_utils::apply_and_check(expression, &["fix"], &["check"]).unwrap();

assert!(
!check_output.contains("Avoid repeated keys in attribute sets"),
"repeated_keys warning remained after fix\nfixed:\n{fixed}\ncheck output:\n{check_output}",
);
Comment on lines +90 to +93
Copy link
Copy Markdown
Collaborator

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?

}
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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, shouldn't this fail to parse?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I have corrected this in master. Mind rebasing?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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!

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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.
───╯
Loading