Skip to content

Commit a911ce5

Browse files
authored
feat: Add test harness (#11)
1 parent 005bdab commit a911ce5

12 files changed

Lines changed: 452 additions & 9 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The entire interface can be summarized by the following excerpt of the manpage:
1111
ns [OPTION]… [SOURCE]… [TARGET_DIR]
1212
```
1313

14+
> <pre>
1415
> Each SOURCE is positional and may be one of:
1516
>
1617
> URI#ATTR select a devshell or package from a flake
@@ -23,6 +24,7 @@ ns [OPTION]… [SOURCE]… [TARGET_DIR]
2324
>
2425
> If a single SOURCE is provided and it is a directory, ns switches into
2526
> it by default unless a TARGET_DIR is explicitly given.
27+
> </pre>
2628
2729
Try it from anywhere:
2830

bin/main.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ open Ns
22
open Ns.Cmd
33
open Ns.Util
44

5-
let main ({ installables; target_info } : Cli.strategy) =
5+
let main ({ installables; target_info; printcmd } : Cli.strategy) =
66
let ({ entrypoint; attribute; subshell_dir } : Cli.target_info) = target_info in
77
Option.value ~default:(Option.value ~default:(Sys.getcwd ()) entrypoint) subshell_dir
88
|> Unix.cd;
@@ -26,7 +26,7 @@ let main ({ installables; target_info } : Cli.strategy) =
2626
|>+ [ "--command"; Unix.shell ]
2727
| None -> Cmd.builder "nix" |>+ [ "shell" ] |>+ installables
2828
in
29-
ignore (Cmd.run cmd)
29+
if printcmd then print_endline (Cmd.to_string cmd) else ignore (Cmd.run cmd)
3030
;;
3131

3232
Cli.eval main

dune-project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
(name ns)
44

5-
(version 0.4.0)
5+
(version 0.4.1)
66

77
(generate_opam_files true)
88

flake.nix

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
doCheck = true;
4141
nativeCheckInputs = attrValues {
4242
inherit (pkgs) ocamlformat;
43+
inherit (pkgs) rustc clippy rustfmt;
4344
};
4445
checkPhase = ''
4546
red() {
@@ -50,9 +51,24 @@
5051
printf "$(red "Error:") Generated opam file does not match provided opam file\n"
5152
exit 1
5253
fi
54+
if ! dune test --force --no-buffer --display=quiet; then
55+
printf "\n"
56+
printf "$(red "Error:") dune test reported test failures\n"
57+
exit 1
58+
fi
59+
if ! clippy-driver --test "${finalAttrs.src}/test/test_ns.rs" -Dwarnings; then
60+
printf "\n"
61+
printf "$(red "Error:") test.rs contains warnings\n"
62+
exit 1
63+
fi
5364
if ! dune fmt --diff-command="diff --color=always"; then
5465
printf "\n"
55-
printf "$(red "Error:") Some files are not properly formatted\n"
66+
printf "$(red "Error:") Some OCaml files are not properly formatted\n"
67+
exit 1
68+
fi
69+
if ! rustfmt --check --color=always "${finalAttrs.src}/test/test_ns.rs"; then
70+
printf "\n"
71+
printf "$(red "Error:") test.rs is not properly formatted\n"
5672
exit 1
5773
fi
5874
'';
@@ -78,14 +94,17 @@
7894
default = pkgs.mkShell {
7995
inputsFrom = [ self.packages.${pkgs.stdenv.hostPlatform.system}.default ];
8096
packages = attrValues {
81-
inherit (pkgs) ocamlformat nil;
97+
inherit (pkgs) ocamlformat nil rustc clippy rustfmt;
8298
inherit (pkgs.ocamlPackages) ocaml-lsp odoc;
8399
};
84100
shellHook = ''
85101
dune build # Ensure build artifacts exist for LSP
86102
'';
87103
};
88104
});
105+
checks = eachSystem (pkgs: {
106+
inherit (self.packages.${pkgs.stdenv.hostPlatform.system}) default;
107+
});
89108
formatter = eachSystem (pkgs: pkgs.nixpkgs-fmt);
90109
};
91110
}

lib/cli.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ module Cli = struct
1717
type strategy =
1818
{ installables : string list
1919
; target_info : target_info
20+
; printcmd : bool
2021
}
2122

2223
(** Positional URI arguments passed to the program via the command line. *)
2324
let uris = Arg.(value & pos_all dirpath [] & info [])
2425

26+
let printcmd =
27+
let doc = "Print the command to stdout instead of executing it." in
28+
Arg.(value & flag & info [ "printcmd" ] ~doc)
29+
;;
30+
2531
(** Processes the args so that the main function knows what to do with them. *)
2632
let make_strategy =
27-
let build original_args =
33+
let build original_args printcmd =
2834
let installables, target_info =
2935
let default_installables = []
3036
and default_target =
@@ -81,9 +87,9 @@ module Cli = struct
8187
, { entrypoint = None; attribute = None; subshell_dir = Some subshell_dir } )
8288
| _ -> Uri.parse_targets_tr original_args, default_target)
8389
in
84-
{ installables; target_info }
90+
{ installables; target_info; printcmd }
8591
in
86-
Term.(const build $ uris)
92+
Term.(const build $ uris $ printcmd)
8793
;;
8894

8995
let cmd entrypoint =
@@ -104,6 +110,8 @@ module Cli = struct
104110
; `P
105111
"If a single SOURCE is provided and it is a directory, ns switches into it by \
106112
default unless a TARGET_DIR is explicitly given."
113+
; `S Manpage.s_options
114+
; `S ""
107115
; `S Manpage.s_common_options
108116
; `S ""
109117
; `S Manpage.s_examples

lib/cmd.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ module Cmd = struct
1616
| args -> List.fold_left (fun cmd arg -> Cmd.add_arg cmd arg) cmd args
1717
;;
1818

19+
(** Convert a command into a string *)
20+
let to_string cmd = Cmd.to_string cmd
21+
1922
(** Run a command
2023
2124
Example:

ns.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is generated by dune, edit dune-project instead
22
opam-version: "2.0"
3-
version: "0.4.0"
3+
version: "0.4.1"
44
synopsis: "A unified interface for nix shell."
55
description:
66
"An intuitive nix shell interface that unifies the `nix shell`, `nix develop` and `nix-shell` commands."

test/dune

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
;; Define the test stanza for dune test
2+
3+
(test
4+
(name test_ns)
5+
(deps test_ns src/flake/flake.nix src/shell/shell.nix)
6+
(flags (:standard)))
7+
8+
;; Rule to compile the Rust test file into a binary
9+
10+
(rule
11+
(targets test_ns) ; the Dune target name
12+
(deps test_ns.rs) ; path relative to this dune file
13+
(action
14+
(run rustc --test %{deps} -o %{targets} --allow warnings)))

test/src/flake/flake.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
3+
outputs = { self, nixpkgs }:
4+
let
5+
inherit (nixpkgs.lib) genAttrs;
6+
inherit (nixpkgs.lib.systems) flakeExposed;
7+
eachSystem = f: genAttrs flakeExposed (system: f (import nixpkgs { inherit system; }));
8+
in
9+
{
10+
packages = eachSystem (pkgs: { default = pkgs.hello; });
11+
};
12+
}

test/src/shell/shell.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{ pkgs ? import <nixpkgs> { } }:
2+
pkgs.mkShell {
3+
buildInputs = builtins.attrValues {
4+
inherit (pkgs) hello;
5+
};
6+
}

0 commit comments

Comments
 (0)