mirror of
https://github.com/nmattia/niv.git
synced 2024-11-22 22:10:46 +03:00
Merge pull request #54 from nmattia/lewo-tests
Add tests by mocking GitHub API
This commit is contained in:
commit
9ef6a381fc
@ -29,6 +29,9 @@ with rec
|
||||
rec
|
||||
{ inherit niv-source niv-devshell;
|
||||
inherit (haskellPackages) niv;
|
||||
|
||||
tests = pkgs.callPackage ./tests { inherit niv; };
|
||||
|
||||
readme = pkgs.writeText "README.md"
|
||||
(with
|
||||
{ template = builtins.readFile ./README.tpl.md;
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
2412
tests/data/repos/NixOS/nixpkgs-channels/commits.json
Normal file
2412
tests/data/repos/NixOS/nixpkgs-channels/commits.json
Normal file
File diff suppressed because it is too large
Load Diff
122
tests/data/repos/NixOS/nixpkgs-channels/repository.json
Normal file
122
tests/data/repos/NixOS/nixpkgs-channels/repository.json
Normal file
@ -0,0 +1,122 @@
|
||||
{
|
||||
"id": 31716917,
|
||||
"node_id": "MDEwOlJlcG9zaXRvcnkzMTcxNjkxNw==",
|
||||
"name": "nixpkgs-channels",
|
||||
"full_name": "NixOS/nixpkgs-channels",
|
||||
"private": false,
|
||||
"owner": {
|
||||
"login": "NixOS",
|
||||
"id": 487568,
|
||||
"node_id": "MDEyOk9yZ2FuaXphdGlvbjQ4NzU2OA==",
|
||||
"avatar_url": "https://avatars3.githubusercontent.com/u/487568?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/NixOS",
|
||||
"html_url": "https://github.com/NixOS",
|
||||
"followers_url": "https://api.github.com/users/NixOS/followers",
|
||||
"following_url": "https://api.github.com/users/NixOS/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/NixOS/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/NixOS/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/NixOS/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/NixOS/orgs",
|
||||
"repos_url": "https://api.github.com/users/NixOS/repos",
|
||||
"events_url": "https://api.github.com/users/NixOS/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/NixOS/received_events",
|
||||
"type": "Organization",
|
||||
"site_admin": false
|
||||
},
|
||||
"html_url": "https://github.com/NixOS/nixpkgs-channels",
|
||||
"description": "Nixpkgs/NixOS branches that track the Nixpkgs/NixOS channels",
|
||||
"fork": false,
|
||||
"url": "https://api.github.com/repos/NixOS/nixpkgs-channels",
|
||||
"forks_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/forks",
|
||||
"keys_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/keys{/key_id}",
|
||||
"collaborators_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/collaborators{/collaborator}",
|
||||
"teams_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/teams",
|
||||
"hooks_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/hooks",
|
||||
"issue_events_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/issues/events{/number}",
|
||||
"events_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/events",
|
||||
"assignees_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/assignees{/user}",
|
||||
"branches_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/branches{/branch}",
|
||||
"tags_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/tags",
|
||||
"blobs_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/git/blobs{/sha}",
|
||||
"git_tags_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/git/tags{/sha}",
|
||||
"git_refs_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/git/refs{/sha}",
|
||||
"trees_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/git/trees{/sha}",
|
||||
"statuses_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/statuses/{sha}",
|
||||
"languages_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/languages",
|
||||
"stargazers_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/stargazers",
|
||||
"contributors_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/contributors",
|
||||
"subscribers_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/subscribers",
|
||||
"subscription_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/subscription",
|
||||
"commits_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/commits{/sha}",
|
||||
"git_commits_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/git/commits{/sha}",
|
||||
"comments_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/comments{/number}",
|
||||
"issue_comment_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/issues/comments{/number}",
|
||||
"contents_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/contents/{+path}",
|
||||
"compare_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/compare/{base}...{head}",
|
||||
"merges_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/merges",
|
||||
"archive_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/{archive_format}{/ref}",
|
||||
"downloads_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/downloads",
|
||||
"issues_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/issues{/number}",
|
||||
"pulls_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/pulls{/number}",
|
||||
"milestones_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/milestones{/number}",
|
||||
"notifications_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/notifications{?since,all,participating}",
|
||||
"labels_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/labels{/name}",
|
||||
"releases_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/releases{/id}",
|
||||
"deployments_url": "https://api.github.com/repos/NixOS/nixpkgs-channels/deployments",
|
||||
"created_at": "2015-03-05T14:12:05Z",
|
||||
"updated_at": "2019-04-25T16:29:51Z",
|
||||
"pushed_at": "2019-04-25T20:34:53Z",
|
||||
"git_url": "git://github.com/NixOS/nixpkgs-channels.git",
|
||||
"ssh_url": "git@github.com:NixOS/nixpkgs-channels.git",
|
||||
"clone_url": "https://github.com/NixOS/nixpkgs-channels.git",
|
||||
"svn_url": "https://github.com/NixOS/nixpkgs-channels",
|
||||
"homepage": null,
|
||||
"size": 515009,
|
||||
"stargazers_count": 115,
|
||||
"watchers_count": 115,
|
||||
"language": "Nix",
|
||||
"has_issues": false,
|
||||
"has_projects": true,
|
||||
"has_downloads": true,
|
||||
"has_wiki": false,
|
||||
"has_pages": false,
|
||||
"forks_count": 66,
|
||||
"mirror_url": null,
|
||||
"archived": false,
|
||||
"disabled": false,
|
||||
"open_issues_count": 0,
|
||||
"license": {
|
||||
"key": "mit",
|
||||
"name": "MIT License",
|
||||
"spdx_id": "MIT",
|
||||
"url": "https://api.github.com/licenses/mit",
|
||||
"node_id": "MDc6TGljZW5zZTEz"
|
||||
},
|
||||
"forks": 66,
|
||||
"open_issues": 0,
|
||||
"watchers": 115,
|
||||
"default_branch": "nixos-unstable",
|
||||
"organization": {
|
||||
"login": "NixOS",
|
||||
"id": 487568,
|
||||
"node_id": "MDEyOk9yZ2FuaXphdGlvbjQ4NzU2OA==",
|
||||
"avatar_url": "https://avatars3.githubusercontent.com/u/487568?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/NixOS",
|
||||
"html_url": "https://github.com/NixOS",
|
||||
"followers_url": "https://api.github.com/users/NixOS/followers",
|
||||
"following_url": "https://api.github.com/users/NixOS/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/NixOS/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/NixOS/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/NixOS/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/NixOS/orgs",
|
||||
"repos_url": "https://api.github.com/users/NixOS/repos",
|
||||
"events_url": "https://api.github.com/users/NixOS/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/NixOS/received_events",
|
||||
"type": "Organization",
|
||||
"site_admin": false
|
||||
},
|
||||
"network_count": 66,
|
||||
"subscribers_count": 30
|
||||
}
|
2432
tests/data/repos/nmattia/niv/commits.json
Normal file
2432
tests/data/repos/nmattia/niv/commits.json
Normal file
File diff suppressed because it is too large
Load Diff
96
tests/data/repos/nmattia/niv/repository.json
Normal file
96
tests/data/repos/nmattia/niv/repository.json
Normal file
@ -0,0 +1,96 @@
|
||||
{
|
||||
"id": 166296150,
|
||||
"node_id": "MDEwOlJlcG9zaXRvcnkxNjYyOTYxNTA=",
|
||||
"name": "niv",
|
||||
"full_name": "nmattia/niv",
|
||||
"private": false,
|
||||
"owner": {
|
||||
"login": "nmattia",
|
||||
"id": 6930756,
|
||||
"node_id": "MDQ6VXNlcjY5MzA3NTY=",
|
||||
"avatar_url": "https://avatars2.githubusercontent.com/u/6930756?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/nmattia",
|
||||
"html_url": "https://github.com/nmattia",
|
||||
"followers_url": "https://api.github.com/users/nmattia/followers",
|
||||
"following_url": "https://api.github.com/users/nmattia/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/nmattia/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/nmattia/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/nmattia/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/nmattia/orgs",
|
||||
"repos_url": "https://api.github.com/users/nmattia/repos",
|
||||
"events_url": "https://api.github.com/users/nmattia/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/nmattia/received_events",
|
||||
"type": "User",
|
||||
"site_admin": false
|
||||
},
|
||||
"html_url": "https://github.com/nmattia/niv",
|
||||
"description": "Easy dependency management for Nix projects",
|
||||
"fork": false,
|
||||
"url": "https://api.github.com/repos/nmattia/niv",
|
||||
"forks_url": "https://api.github.com/repos/nmattia/niv/forks",
|
||||
"keys_url": "https://api.github.com/repos/nmattia/niv/keys{/key_id}",
|
||||
"collaborators_url": "https://api.github.com/repos/nmattia/niv/collaborators{/collaborator}",
|
||||
"teams_url": "https://api.github.com/repos/nmattia/niv/teams",
|
||||
"hooks_url": "https://api.github.com/repos/nmattia/niv/hooks",
|
||||
"issue_events_url": "https://api.github.com/repos/nmattia/niv/issues/events{/number}",
|
||||
"events_url": "https://api.github.com/repos/nmattia/niv/events",
|
||||
"assignees_url": "https://api.github.com/repos/nmattia/niv/assignees{/user}",
|
||||
"branches_url": "https://api.github.com/repos/nmattia/niv/branches{/branch}",
|
||||
"tags_url": "https://api.github.com/repos/nmattia/niv/tags",
|
||||
"blobs_url": "https://api.github.com/repos/nmattia/niv/git/blobs{/sha}",
|
||||
"git_tags_url": "https://api.github.com/repos/nmattia/niv/git/tags{/sha}",
|
||||
"git_refs_url": "https://api.github.com/repos/nmattia/niv/git/refs{/sha}",
|
||||
"trees_url": "https://api.github.com/repos/nmattia/niv/git/trees{/sha}",
|
||||
"statuses_url": "https://api.github.com/repos/nmattia/niv/statuses/{sha}",
|
||||
"languages_url": "https://api.github.com/repos/nmattia/niv/languages",
|
||||
"stargazers_url": "https://api.github.com/repos/nmattia/niv/stargazers",
|
||||
"contributors_url": "https://api.github.com/repos/nmattia/niv/contributors",
|
||||
"subscribers_url": "https://api.github.com/repos/nmattia/niv/subscribers",
|
||||
"subscription_url": "https://api.github.com/repos/nmattia/niv/subscription",
|
||||
"commits_url": "https://api.github.com/repos/nmattia/niv/commits{/sha}",
|
||||
"git_commits_url": "https://api.github.com/repos/nmattia/niv/git/commits{/sha}",
|
||||
"comments_url": "https://api.github.com/repos/nmattia/niv/comments{/number}",
|
||||
"issue_comment_url": "https://api.github.com/repos/nmattia/niv/issues/comments{/number}",
|
||||
"contents_url": "https://api.github.com/repos/nmattia/niv/contents/{+path}",
|
||||
"compare_url": "https://api.github.com/repos/nmattia/niv/compare/{base}...{head}",
|
||||
"merges_url": "https://api.github.com/repos/nmattia/niv/merges",
|
||||
"archive_url": "https://api.github.com/repos/nmattia/niv/{archive_format}{/ref}",
|
||||
"downloads_url": "https://api.github.com/repos/nmattia/niv/downloads",
|
||||
"issues_url": "https://api.github.com/repos/nmattia/niv/issues{/number}",
|
||||
"pulls_url": "https://api.github.com/repos/nmattia/niv/pulls{/number}",
|
||||
"milestones_url": "https://api.github.com/repos/nmattia/niv/milestones{/number}",
|
||||
"notifications_url": "https://api.github.com/repos/nmattia/niv/notifications{?since,all,participating}",
|
||||
"labels_url": "https://api.github.com/repos/nmattia/niv/labels{/name}",
|
||||
"releases_url": "https://api.github.com/repos/nmattia/niv/releases{/id}",
|
||||
"deployments_url": "https://api.github.com/repos/nmattia/niv/deployments",
|
||||
"created_at": "2019-01-17T21:00:18Z",
|
||||
"updated_at": "2019-04-23T09:28:07Z",
|
||||
"pushed_at": "2019-04-25T22:20:50Z",
|
||||
"git_url": "git://github.com/nmattia/niv.git",
|
||||
"ssh_url": "git@github.com:nmattia/niv.git",
|
||||
"clone_url": "https://github.com/nmattia/niv.git",
|
||||
"svn_url": "https://github.com/nmattia/niv",
|
||||
"homepage": "https://github.com/nmattia/niv",
|
||||
"size": 125,
|
||||
"stargazers_count": 84,
|
||||
"watchers_count": 84,
|
||||
"language": "Haskell",
|
||||
"has_issues": true,
|
||||
"has_projects": true,
|
||||
"has_downloads": true,
|
||||
"has_wiki": true,
|
||||
"has_pages": false,
|
||||
"forks_count": 5,
|
||||
"mirror_url": null,
|
||||
"archived": false,
|
||||
"disabled": false,
|
||||
"open_issues_count": 9,
|
||||
"license": null,
|
||||
"forks": 5,
|
||||
"open_issues": 9,
|
||||
"watchers": 84,
|
||||
"default_branch": "master",
|
||||
"network_count": 5,
|
||||
"subscribers_count": 9
|
||||
}
|
94
tests/default.nix
Normal file
94
tests/default.nix
Normal file
@ -0,0 +1,94 @@
|
||||
# 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
|
||||
''
|
25
tests/expected/niv-init.json
Normal file
25
tests/expected/niv-init.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"nixpkgs": {
|
||||
"url": "http://localhost:3333/NixOS/nixpkgs-channels/archive/571b40d3f50466d3e91c1e609d372de96d782793.tar.gz",
|
||||
"owner": "NixOS",
|
||||
"branch": "nixos-18.09",
|
||||
"url_template": "http://localhost:3333/<owner>/<repo>/archive/<rev>.tar.gz",
|
||||
"repo": "nixpkgs-channels",
|
||||
"type": "tarball",
|
||||
"sha256": "18hlxki1hbimvm1jagcf7dy5km1iakr1px472cb05y9602py7f6g",
|
||||
"description": "Nixpkgs/NixOS branches that track the Nixpkgs/NixOS channels",
|
||||
"rev": "571b40d3f50466d3e91c1e609d372de96d782793"
|
||||
},
|
||||
"niv": {
|
||||
"homepage": "https://github.com/nmattia/niv",
|
||||
"url": "http://localhost:3333/nmattia/niv/archive/a489b65a5c3a29983701069d1ce395b23d9bde64.tar.gz",
|
||||
"owner": "nmattia",
|
||||
"branch": "master",
|
||||
"url_template": "http://localhost:3333/<owner>/<repo>/archive/<rev>.tar.gz",
|
||||
"repo": "niv",
|
||||
"type": "tarball",
|
||||
"sha256": "18hx0pg48xh306gwz9wja8fcyl8isfkv3ja7453yj3hmy72b6scp",
|
||||
"description": "Easy dependency management for Nix projects",
|
||||
"rev": "a489b65a5c3a29983701069d1ce395b23d9bde64"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user