lol

Merge pull request #263096 from hercules-ci/clean-up-data-writers

Clean up data writers

authored by

Robert Hensing and committed by
GitHub
c1dc59dc 2b8b855f

+12 -32
+7 -23
pkgs/build-support/writers/data.nix
··· 1 - { lib, runCommand, dasel }: 1 + { lib, pkgs, formats, runCommand, dasel }: 2 2 let 3 3 daselBin = lib.getExe dasel; 4 4 ··· 23 23 # writeJSON = makeDataWriter { input = builtins.toJSON; output = "cp $inputPath $out"; }; 24 24 # myConfig = writeJSON "config.json" { hello = "world"; } 25 25 # 26 - makeDataWriter = { input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data: 26 + makeDataWriter = lib.warn "pkgs.writers.makeDataWriter is deprecated. Use pkgs.writeTextFile." ({ input ? lib.id, output ? "cp $inputPath $out" }: nameOrPath: data: 27 27 assert lib.or (types.path.check nameOrPath) (builtins.match "([0-9A-Za-z._])[0-9A-Za-z._-]*" nameOrPath != null); 28 28 let 29 29 name = last (builtins.split "/" nameOrPath); ··· 40 40 mkdir -p $out/$(dirname "${nameOrPath}") 41 41 mv tmp $out/${nameOrPath} 42 42 ''} 43 - ''; 43 + ''); 44 44 45 - # Writes the content to text. 46 - # 47 - # Example: 48 - # writeText "filename.txt" "file content" 49 - writeText = makeDataWriter { 50 - input = toString; 51 - output = "cp $inputPath $out"; 52 - }; 45 + inherit (pkgs) writeText; 53 46 54 47 # Writes the content to a JSON file. 55 48 # 56 49 # Example: 57 50 # writeJSON "data.json" { hello = "world"; } 58 - writeJSON = makeDataWriter { 59 - input = builtins.toJSON; 60 - output = "${daselBin} -f $inputPath -r json -w json > $out"; 61 - }; 51 + writeJSON = (pkgs.formats.json {}).generate; 62 52 63 53 # Writes the content to a TOML file. 64 54 # 65 55 # Example: 66 56 # writeTOML "data.toml" { hello = "world"; } 67 - writeTOML = makeDataWriter { 68 - input = builtins.toJSON; 69 - output = "${daselBin} -f $inputPath -r json -w toml > $out"; 70 - }; 57 + writeTOML = (pkgs.formats.toml {}).generate; 71 58 72 59 # Writes the content to a YAML file. 73 60 # 74 61 # Example: 75 62 # writeYAML "data.yaml" { hello = "world"; } 76 - writeYAML = makeDataWriter { 77 - input = builtins.toJSON; 78 - output = "${daselBin} -f $inputPath -r json -w yaml > $out"; 79 - }; 63 + writeYAML = (pkgs.formats.yaml {}).generate; 80 64 }
+5 -9
pkgs/build-support/writers/test.nix
··· 7 7 , python3Packages 8 8 , pypy3Packages 9 9 , runCommand 10 + , testers 10 11 , writers 11 12 , writeText 12 13 }: ··· 36 37 let 37 38 expectedFile = writeText "${file.name}-expected" expected; 38 39 in 39 - runCommand "run-${file.name}" {} '' 40 - if ! diff -u ${file} ${expectedFile}; then 41 - echo 'test ${file.name} failed' 42 - exit 1 43 - fi 44 - 45 - touch $out 46 - ''; 40 + testers.testEqualContents { expected = expectedFile; actual = file; assertion = "${file.name} matches"; }; 47 41 in 48 42 lib.recurseIntoAttrs { 49 43 bin = lib.recurseIntoAttrs { ··· 261 255 262 256 toml = expectDataEqual { 263 257 file = writeTOML "data.toml" { hello = "world"; }; 264 - expected = "hello = 'world'\n"; 258 + expected = '' 259 + hello = "world" 260 + ''; 265 261 }; 266 262 267 263 yaml = expectDataEqual {