Merge pull request #8 from nix-community/tests

Tests
This commit is contained in:
Jörg Thalheim 2020-04-01 16:17:51 +01:00 committed by GitHub
commit 97ed44cc73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 120 additions and 7 deletions

12
.github/workflows/test.yml vendored Normal file
View file

@ -0,0 +1,12 @@
name: "Test"
on:
pull_request:
push:
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v7
- run:
nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/nixpkgs-unstable.tar.gz ci.nix --run 'true'

View file

@ -1,5 +1,7 @@
# nix-direnv
![Test](https://github.com/nix-community/nix-direnv/workflows/Test/badge.svg)
A fast, persistent use_nix implementation for direnv.
Prominent features:

21
ci.nix Normal file
View file

@ -0,0 +1,21 @@
with import <nixpkgs> {};
mkShell {
nativeBuildInputs = [
shellcheck direnv mypy python3.pkgs.black python3.pkgs.flake8
];
shellHook = ''
set -e
echo -e "\x1b[32m## run shellcheck\x1b[0m"
LC_ALL=en_US.utf-8 black --check .
echo -e "\x1b[32m## run black\x1b[0m"
LC_ALL=en_US.utf-8 black --check .
echo -e "\x1b[32m## run flake8\x1b[0m"
flake8 --ignore E501 tests
echo -e "\x1b[32m## run mypy\x1b[0m"
mypy tests
echo -e "\x1b[32m## run unittest\x1b[0m"
${pkgs.python3.interpreter} -m unittest discover tests
'';
}

26
default.nix Normal file
View file

@ -0,0 +1,26 @@
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
stdenv.mkDerivation {
name = "nix-direnv";
src = ./.;
postPatch = ''
substituteInPlace direnvrc \
--replace "grep" "${gnugrep}/bin/grep" \
--replace "nix-shell" "${nix}/bin/nix-shell" \
--replace "nix-instantiate" "${nix}/bin/nix-shell"
'';
installPhase = ''
install -m500 -D direnvrc $out/share/nix-direnv/direnvrc
'';
meta = with stdenv.lib; {
description = "A fast, persistent use_nix implementation for direnv";
homepage = "https://github.com/nix-community/nix-direnv";
license = licenses.mit;
platforms = platforms.unix;
};
}

View file

@ -1,5 +1,4 @@
#!/usr/bin/env bash
# shellcheck shell=bash
use_nix() {
local path
@ -43,7 +42,7 @@ use_nix() {
log_status using cached derivation
fi
local term_backup=$TERM path_backup=$PATH
if [[ ! -z ${TMPDIR+x} ]]; then
if [[ -n ${TMPDIR+x} ]]; then
local tmp_backup=$TMPDIR
fi
@ -53,8 +52,8 @@ use_nix() {
log_status eval "$cache"
read -r cache_content < "$cache"
eval "$cache_content"
export PATH=$PATH:$path_backup TERM=$term_backup
if [[ ! -z ${tmp_backup+x} ]]; then
export PATH=$PATH:$path_backup TERM=$term_backup
if [[ -n ${tmp_backup+x} ]]; then
export TMPDIR=${tmp_backup}
else
unset TMPDIR
@ -62,14 +61,14 @@ use_nix() {
# `nix-shell --pure` sets invalid ssl certificate paths
if [[ "${SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
if [[ ! -z ${impure_ssl_cert_file+x} ]]; then
if [[ -n ${impure_ssl_cert_file+x} ]]; then
export SSL_CERT_FILE=${impure_ssl_cert_file}
else
unset SSL_CERT_FILE
fi
fi
if [[ "${NIX_SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then
if [[ ! -z ${impure_nix_ssl_cert_file+x} ]]; then
if [[ -n ${impure_nix_ssl_cert_file+x} ]]; then
export NIX_SSL_CERT_FILE=${impure_nix_ssl_cert_file}
else
unset NIX_SSL_CERT_FILE

8
shell.nix Normal file
View file

@ -0,0 +1,8 @@
with import <nixpkgs> {};
mkShell {
nativeBuildInputs = [
python3
shellcheck
direnv
];
}

39
tests/test.py Normal file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env python2
from tempfile import TemporaryDirectory
import os
import subprocess
from pathlib import Path
import shutil
import unittest
TEST_ROOT = Path(__file__).resolve().parent
class IntegrationTest(unittest.TestCase):
def setUp(self) -> None:
self.env = os.environ.copy()
self.dir = TemporaryDirectory()
self.env["HOME"] = str(self.dir.name)
self.testenv = Path(self.dir.name).joinpath("testenv")
shutil.copytree(TEST_ROOT.joinpath("testenv"), self.testenv)
direnvrc = str(TEST_ROOT.parent.joinpath("direnvrc"))
with open(self.testenv.joinpath(".envrc"), "w") as f:
f.write(f"source {direnvrc}\n")
f.write(f"use nix")
def test_direnv(self) -> None:
subprocess.run(
["direnv", "allow"], cwd=str(self.testenv), env=self.env, check=True
)
subprocess.run(
["direnv", "exec", str(self.testenv), "hello"], env=self.env, check=True
)
def tearDown(self) -> None:
self.dir.cleanup()
if __name__ == "__main__":
unittest.main()

2
tests/testenv/.envrc Normal file
View file

@ -0,0 +1,2 @@
source ../../direnvrc
use nix

4
tests/testenv/shell.nix Normal file
View file

@ -0,0 +1,4 @@
with import <nixpkgs> {};
mkShell {
nativeBuildInputs = [ hello ];
}