From e7fce0da51f9f3b4d311efd99061af7e49982edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 1 Apr 2020 14:48:31 +0100 Subject: [PATCH 1/4] shellcheck fixes --- direnvrc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/direnvrc b/direnvrc index cc1068a..c015557 100644 --- a/direnvrc +++ b/direnvrc @@ -43,7 +43,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 @@ -54,7 +54,7 @@ use_nix() { read -r cache_content < "$cache" eval "$cache_content" export PATH=$PATH:$path_backup TERM=$term_backup - if [[ ! -z ${tmp_backup+x} ]]; then + if [[ -n ${tmp_backup+x} ]]; then export TMPDIR=${tmp_backup} else unset TMPDIR @@ -62,14 +62,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 From ea7c07e7833dad89094a11f6514a0f94e37526d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 1 Apr 2020 15:46:46 +0100 Subject: [PATCH 2/4] replace shebang by shellcheck directive --- direnvrc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/direnvrc b/direnvrc index c015557..db427c2 100644 --- a/direnvrc +++ b/direnvrc @@ -1,5 +1,4 @@ -#!/usr/bin/env bash - +# shellcheck shell=bash use_nix() { local path @@ -53,7 +52,7 @@ use_nix() { log_status eval "$cache" read -r cache_content < "$cache" eval "$cache_content" - export PATH=$PATH:$path_backup TERM=$term_backup + export PATH=$PATH:$path_backup TERM=$term_backup if [[ -n ${tmp_backup+x} ]]; then export TMPDIR=${tmp_backup} else From 862b772839ebd6d2b2a3f7456235b2ce6a055d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 1 Apr 2020 15:25:32 +0100 Subject: [PATCH 3/4] add tests --- .github/workflows/test.yml | 12 ++++++++++++ ci.nix | 21 ++++++++++++++++++++ default.nix | 26 +++++++++++++++++++++++++ shell.nix | 8 ++++++++ tests/test.py | 39 ++++++++++++++++++++++++++++++++++++++ tests/testenv/.envrc | 2 ++ tests/testenv/shell.nix | 4 ++++ 7 files changed, 112 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 ci.nix create mode 100644 default.nix create mode 100644 shell.nix create mode 100644 tests/test.py create mode 100644 tests/testenv/.envrc create mode 100644 tests/testenv/shell.nix diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..534f1ca --- /dev/null +++ b/.github/workflows/test.yml @@ -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' diff --git a/ci.nix b/ci.nix new file mode 100644 index 0000000..a30acf5 --- /dev/null +++ b/ci.nix @@ -0,0 +1,21 @@ +with import {}; +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 + ''; +} diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..24a8428 --- /dev/null +++ b/default.nix @@ -0,0 +1,26 @@ +{ pkgs ? import {} }: + +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; + }; +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..3c89d29 --- /dev/null +++ b/shell.nix @@ -0,0 +1,8 @@ +with import {}; +mkShell { + nativeBuildInputs = [ + python3 + shellcheck + direnv + ]; +} diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 0000000..a9d7a20 --- /dev/null +++ b/tests/test.py @@ -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() diff --git a/tests/testenv/.envrc b/tests/testenv/.envrc new file mode 100644 index 0000000..a565315 --- /dev/null +++ b/tests/testenv/.envrc @@ -0,0 +1,2 @@ +source ../../direnvrc +use nix diff --git a/tests/testenv/shell.nix b/tests/testenv/shell.nix new file mode 100644 index 0000000..76a5461 --- /dev/null +++ b/tests/testenv/shell.nix @@ -0,0 +1,4 @@ +with import {}; +mkShell { + nativeBuildInputs = [ hello ]; +} From b366328f7471270dc879c31e466d10f0a1ee8d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 1 Apr 2020 16:17:18 +0100 Subject: [PATCH 4/4] README.md: add test badge --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b879385..f1d480a 100644 --- a/README.md +++ b/README.md @@ -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: