1
1
mirror of https://github.com/nmattia/niv.git synced 2024-11-09 12:38:31 +03:00
niv/tests/default.nix
Antoine Eiche 64f8845e51 Add tests by mocking GitHub API
The following scenario is tested:
1. niv init
2. niv drop niv
3. niv add nmattia/niv
4. niv update niv
5. niv add foo -v 1 -t 'localhost:3333/foo-v<version>'
2019-04-29 12:06:41 +02:00

95 lines
3.8 KiB
Nix

# Test the niv binary
#
# A webserver is started to mock API requests to GitHub and to serve
# files. niv commands are then executed and the some elements of the
# resulting sources.json are verified with jq.
#
# Note that niv needs to be patched to use http://localhost:3333
# instead of https://github.com since it doesn't support another
# GitHub url (while the GitHub API permits that).
#
# To mock GitHub, JSON responses of the real GitHub API have been
# copied to files (in the data folder) and are used by the webserver
# to serve GitHub API responses.
{ pkgs, niv }:
let
nivForTest = niv.overrideDerivation(old: {
# TODO: Remove this patch by adding an argument to the github
# subcommand to support GitHub entreprise.
prePatch = ''
sed "s|GH.executeRequest'|GH.executeRequest (GH.EnterpriseOAuth \"http://localhost:3333\" \"\")|" -i app/Niv.hs
sed "s|https://github.com|http://localhost:3333|" -i app/Niv.hs
'';
});
in pkgs.runCommand "test" { buildInputs = [ pkgs.python nivForTest pkgs.nix pkgs.jq pkgs.netcat-gnu ]; }
''
set -euo pipefail
echo *** Starting the webserver...
mkdir mock
( cd mock; python -m SimpleHTTPServer 3333 ) &
while ! nc -z 127.0.0.1 3333; do
echo waiting for mock server
sleep 1
done
# We can't access /nix or /var from the sandbox
export NIX_STATE_DIR=$PWD/nix/var/nix
export NIX_STORE_DIR=$PWD/nix/store
echo -e "\n*** niv init"
mkdir -p mock/repos/nmattia/niv/
cp ${./data/repos/nmattia/niv/repository.json} mock/repos/nmattia/niv/index.html
cat ${./data/repos/nmattia/niv/commits.json} | jq '.[0] | [.]' > mock/repos/nmattia/niv/commits
mkdir -p mock/nmattia/niv/archive
cp ${./data/archives/a489b65a5c3a29983701069d1ce395b23d9bde64.tar.gz} mock/nmattia/niv/archive/a489b65a5c3a29983701069d1ce395b23d9bde64.tar.gz
mkdir -p mock/repos/NixOS/nixpkgs-channels
cp ${./data/repos/NixOS/nixpkgs-channels/repository.json} mock/repos/NixOS/nixpkgs-channels/index.html
cat ${./data/repos/NixOS/nixpkgs-channels/commits.json} | jq '.[0] | [.]' > mock/repos/NixOS/nixpkgs-channels/commits
mkdir -p mock/NixOS/nixpkgs-channels/archive
cp ${./data/archives/571b40d3f50466d3e91c1e609d372de96d782793.tar.gz} mock/NixOS/nixpkgs-channels/archive/571b40d3f50466d3e91c1e609d372de96d782793.tar.gz
niv init
diff -h ${./expected/niv-init.json} nix/sources.json
echo "*** ok."
echo -e "\n*** niv drop niv"
niv drop niv
echo -n "niv package has been removed: "
cat nix/sources.json | jq -e '. | has("niv") | not'
echo -e "*** ok."
echo -e "\n*** niv add nmattia/niv"
# We use the HEAD~1 commit to update it in the next step
cat ${./data/repos/nmattia/niv/commits.json} | jq '.[1] | [.]' > mock/repos/nmattia/niv/commits
cp ${./data/archives/abc51449406ba3279c466b4d356b4ae8522ceb58.tar.gz} mock/nmattia/niv/archive/abc51449406ba3279c466b4d356b4ae8522ceb58.tar.gz
niv add nmattia/niv
echo -n "niv.rev == abc51449406ba3279c466b4d356b4ae8522ceb58: "
cat nix/sources.json | jq -e '.niv | .rev == "abc51449406ba3279c466b4d356b4ae8522ceb58"'
echo -e "*** ok."
echo -e "\n*** niv update niv"
cat ${./data/repos/nmattia/niv/commits.json} | jq '.[0] | [.]' > mock/repos/nmattia/niv/commits
niv update niv
echo -n "niv.rev == a489b65a5c3a29983701069d1ce395b23d9bde64: "
cat nix/sources.json | jq -e '.niv | .rev == "a489b65a5c3a29983701069d1ce395b23d9bde64"'
echo -e "*** ok."
echo -e "\n*** niv add foo -v 1 -t 'localhost:3333/foo-v<version>'"
echo foo > mock/foo-v1
niv add foo -v 1 -t 'localhost:3333/foo-v<version>'
echo -n "foo.url == localhost:3333/foo-v1: "
cat nix/sources.json | jq -e '.foo | .url == "localhost:3333/foo-v1"'
echo "*** ok."
cp nix/sources.json $out
''