From 79130267d31bed7f88236b4245f0da3d187fef26 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 24 Oct 2023 11:01:19 +0200 Subject: [PATCH 1/3] writers/data.nix: Remove duplicate implementations --- pkgs/build-support/writers/data.nix | 28 ++++++---------------------- pkgs/build-support/writers/test.nix | 4 +++- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/pkgs/build-support/writers/data.nix b/pkgs/build-support/writers/data.nix index 48f9bc547ed39..052e640694e82 100644 --- a/pkgs/build-support/writers/data.nix +++ b/pkgs/build-support/writers/data.nix @@ -1,4 +1,4 @@ -{ lib, runCommand, dasel }: +{ lib, pkgs, formats, runCommand, dasel }: let daselBin = lib.getExe dasel; @@ -40,41 +40,25 @@ rec { mkdir -p $out/$(dirname "${nameOrPath}") mv tmp $out/${nameOrPath} ''} - ''; + ''); - # Writes the content to text. - # - # Example: - # writeText "filename.txt" "file content" - writeText = makeDataWriter { - input = toString; - output = "cp $inputPath $out"; - }; + inherit (pkgs) writeText; # Writes the content to a JSON file. # # Example: # writeJSON "data.json" { hello = "world"; } - writeJSON = makeDataWriter { - input = builtins.toJSON; - output = "${daselBin} -f $inputPath -r json -w json > $out"; - }; + writeJSON = (pkgs.formats.json {}).generate; # Writes the content to a TOML file. # # Example: # writeTOML "data.toml" { hello = "world"; } - writeTOML = makeDataWriter { - input = builtins.toJSON; - output = "${daselBin} -f $inputPath -r json -w toml > $out"; - }; + writeTOML = (pkgs.formats.toml {}).generate; # Writes the content to a YAML file. # # Example: # writeYAML "data.yaml" { hello = "world"; } - writeYAML = makeDataWriter { - input = builtins.toJSON; - output = "${daselBin} -f $inputPath -r json -w yaml > $out"; - }; + writeYAML = (pkgs.formats.yaml {}).generate; } diff --git a/pkgs/build-support/writers/test.nix b/pkgs/build-support/writers/test.nix index 2411f8c03a70b..ba481e6f6251b 100644 --- a/pkgs/build-support/writers/test.nix +++ b/pkgs/build-support/writers/test.nix @@ -261,7 +261,9 @@ lib.recurseIntoAttrs { toml = expectDataEqual { file = writeTOML "data.toml" { hello = "world"; }; - expected = "hello = 'world'\n"; + expected = '' + hello = "world" + ''; }; yaml = expectDataEqual { From e64f987fd6b922cfc0b42d2dd516c8e7924befd4 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 24 Oct 2023 11:01:50 +0200 Subject: [PATCH 2/3] writers.makeDataWriter: Deprecate --- pkgs/build-support/writers/data.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/build-support/writers/data.nix b/pkgs/build-support/writers/data.nix index 052e640694e82..45ed5360eaeba 100644 --- a/pkgs/build-support/writers/data.nix +++ b/pkgs/build-support/writers/data.nix @@ -23,7 +23,7 @@ rec { # writeJSON = makeDataWriter { input = builtins.toJSON; output = "cp $inputPath $out"; }; # myConfig = writeJSON "config.json" { hello = "world"; } # - makeDataWriter = { input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data: + makeDataWriter = lib.warn "pkgs.writers.makeDataWriter is deprecated. Use pkgs.writeTextFile." ({ input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data: assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null); let name = last (builtins.split "/" nameOrPath); From a6622bf00a033921d1c08a38fb9d64b8feafdf56 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 24 Oct 2023 11:16:34 +0200 Subject: [PATCH 3/3] writers/test: Refactor It now also checks the executable bit. --- pkgs/build-support/writers/test.nix | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pkgs/build-support/writers/test.nix b/pkgs/build-support/writers/test.nix index ba481e6f6251b..005daf0be5b48 100644 --- a/pkgs/build-support/writers/test.nix +++ b/pkgs/build-support/writers/test.nix @@ -7,6 +7,7 @@ , python3Packages , pypy3Packages , runCommand +, testers , writers , writeText }: @@ -36,14 +37,7 @@ let let expectedFile = writeText "${file.name}-expected" expected; in - runCommand "run-${file.name}" {} '' - if ! diff -u ${file} ${expectedFile}; then - echo 'test ${file.name} failed' - exit 1 - fi - - touch $out - ''; + testers.testEqualContents { expected = expectedFile; actual = file; assertion = "${file.name} matches"; }; in lib.recurseIntoAttrs { bin = lib.recurseIntoAttrs {