From b9e6711f8220d658a245bbd0143dba983336dc03 Mon Sep 17 00:00:00 2001 From: Colin Date: Fri, 7 Jul 2023 01:47:46 +0000 Subject: [PATCH] WIP: tests for highlighting --- Makefile | 15 ++++++++++++ default.nix | 9 ++++++- flake.lock | 8 +++---- flake.nix | 15 +++--------- queries/highlights.scm | 1 - shell.nix | 4 ++-- tests/config/tree-sitter/config.json | 8 +++++++ tests/highlight/basic | 4 ++++ tests/highlight/basic.spec.html | 16 +++++++++++++ tests/test_highlight | 24 +++++++++++++++++++ .../corpus}/annotation.txt | 0 .../corpus}/payload.txt | 0 .../corpus}/shebang.txt | 0 .../grammar.js | 0 .../package.json | 1 + tree-sitter-nix-shell/queries/highlights.scm | 4 ++++ .../queries}/injections.scm | 0 17 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 Makefile delete mode 100644 queries/highlights.scm create mode 100644 tests/config/tree-sitter/config.json create mode 100644 tests/highlight/basic create mode 100644 tests/highlight/basic.spec.html create mode 100755 tests/test_highlight rename {corpus => tree-sitter-nix-shell/corpus}/annotation.txt (100%) rename {corpus => tree-sitter-nix-shell/corpus}/payload.txt (100%) rename {corpus => tree-sitter-nix-shell/corpus}/shebang.txt (100%) rename grammar.js => tree-sitter-nix-shell/grammar.js (100%) rename package.json => tree-sitter-nix-shell/package.json (93%) create mode 100644 tree-sitter-nix-shell/queries/highlights.scm rename {queries => tree-sitter-nix-shell/queries}/injections.scm (100%) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c4352ec --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CACHEDIR := $(PWD)/cache +build: + cd tree-sitter-nix-shell && tree-sitter generate + +test: build + cd tree-sitter-nix-shell && XDG_CACHE_HOME="${CACHEDIR}" tree-sitter test + XDG_CACHE_HOME="${CACHEDIR}" ./tests/test_highlight basic + +clean: + rm -f tree-sitter-nix-shell/{Cargo.toml,binding.gyp} + rm -rf tree-sitter-nix-shell/{bindings/,src/} + rm -rf "${CACHEDIR}" + + +.PHONY: build test clean diff --git a/default.nix b/default.nix index 9f7aaaa..4302a9d 100644 --- a/default.nix +++ b/default.nix @@ -3,6 +3,13 @@ pkgs.tree-sitter.buildGrammar { src = ./.; version = "0.1.0"; - language = "nixshell"; + language = "nix-shell"; generate = true; + location = "tree-sitter-nix-shell"; + + checkPhase = '' + (cd ..; make test) + ''; + nativeCheckInputs = with pkgs; [ htmlq ]; + doCheck = true; } diff --git a/flake.lock b/flake.lock index 6f0a383..9016b99 100644 --- a/flake.lock +++ b/flake.lock @@ -20,16 +20,16 @@ }, "nixpkgs": { "locked": { - "lastModified": 1688389917, - "narHash": "sha256-RKiK1QeommEsjQ8fLgxt4831x9O6n2gD7wAhVZTrr8M=", + "lastModified": 1688590700, + "narHash": "sha256-ZF055rIUP89cVwiLpG5xkJzx00gEuuGFF60Bs/LM3wc=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "aed4b19d312525ae7ca9bceb4e1efe3357d0e2eb", + "rev": "f292b4964cb71f9dfbbd30dc9f511d6165cd109b", "type": "github" }, "original": { "id": "nixpkgs", - "ref": "nixos-23.05", + "ref": "nixos-unstable", "type": "indirect" } }, diff --git a/flake.nix b/flake.nix index 22d36c1..299b99d 100644 --- a/flake.nix +++ b/flake.nix @@ -1,7 +1,7 @@ { description = "tree-sitter implementation for nix-shell scripts"; inputs = { - nixpkgs.url = "nixpkgs/nixos-23.05"; + nixpkgs.url = "nixpkgs/nixos-unstable"; flake-utils.url = github:numtide/flake-utils; }; @@ -12,18 +12,9 @@ inherit system; }; in rec { - packages.tree-sitter-nixshell = pkgs.callPackage ./default.nix { }; - defaultPackage = packages.tree-sitter-nixshell; + packages.tree-sitter-nix-shell = pkgs.callPackage ./default.nix { }; + defaultPackage = packages.tree-sitter-nix-shell; devShells.default = pkgs.callPackage ./shell.nix { }; - - apps.clean = { - type = "app"; - program = builtins.toString (pkgs.writeShellScript "clean" '' - # remove build artifacts - rm -f Cargo.toml binding.gyp - rm -rf bindings/ src/ - ''); - }; }); } diff --git a/queries/highlights.scm b/queries/highlights.scm deleted file mode 100644 index 118d38e..0000000 --- a/queries/highlights.scm +++ /dev/null @@ -1 +0,0 @@ -(nix_shell_directive) @keyword diff --git a/shell.nix b/shell.nix index f3411f4..7922160 100644 --- a/shell.nix +++ b/shell.nix @@ -1,10 +1,10 @@ { pkgs ? import { } }: let - tree-sitter-nixshell = pkgs.callPackage ./. { }; + tree-sitter-nix-shell = pkgs.callPackage ./. { }; in pkgs.mkShell { - inherit (tree-sitter-nixshell) nativeBuildInputs; + inherit (tree-sitter-nix-shell) nativeBuildInputs; # to manually build, once in this shell, just run # `tree-sitter generate` } diff --git a/tests/config/tree-sitter/config.json b/tests/config/tree-sitter/config.json new file mode 100644 index 0000000..f6106c1 --- /dev/null +++ b/tests/config/tree-sitter/config.json @@ -0,0 +1,8 @@ +{ + "parser-directories": [ + ".." + ], + "theme": { + "keyword": "#cccccc" + } +} diff --git a/tests/highlight/basic b/tests/highlight/basic new file mode 100644 index 0000000..c48bbbe --- /dev/null +++ b/tests/highlight/basic @@ -0,0 +1,4 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i bash -p bash +true +echo "bash-string" diff --git a/tests/highlight/basic.spec.html b/tests/highlight/basic.spec.html new file mode 100644 index 0000000..55a7951 --- /dev/null +++ b/tests/highlight/basic.spec.html @@ -0,0 +1,16 @@ + + + + + + + +
1#!/usr/bin/env nix-shell +
2#!nix-shell -i bash -p bash +
3true +
4echo "bash-string" +
+ + + + diff --git a/tests/test_highlight b/tests/test_highlight new file mode 100755 index 0000000..37fdd98 --- /dev/null +++ b/tests/test_highlight @@ -0,0 +1,24 @@ +#!/bin/sh + +set -eux + +# `tree-sitter test` doesn't work to validate syntax highlighting for two reasons: +# - it identifies a file's language only by its filetype (not shebang/regex), but nix-shell has no filetype +# - it requires the language to support comments, but nix-shell doesn't have those +# +# hence this custom testing tool. +# +# usage: test_highlight +# +# 1. highlights the file at `highlight/`. +# 2. asserts the result's body against `hightlight_spec/.html` + +here=$(dirname $(realpath $0)) +echo $here +input_file="$here/highlight/$1" +spec_file="$here/highlight/$1.spec.html" + +# cd into this directory before highlighting because paths specified in the tree-sitter config are relative to PWD +cd "$here" && XDG_CONFIG_HOME="$here/config" tree-sitter highlight --scope "source.nix_shell" --html "$input_file" | \ + htmlq 'body' | \ + diff -u "$spec_file" '/dev/stdin' diff --git a/corpus/annotation.txt b/tree-sitter-nix-shell/corpus/annotation.txt similarity index 100% rename from corpus/annotation.txt rename to tree-sitter-nix-shell/corpus/annotation.txt diff --git a/corpus/payload.txt b/tree-sitter-nix-shell/corpus/payload.txt similarity index 100% rename from corpus/payload.txt rename to tree-sitter-nix-shell/corpus/payload.txt diff --git a/corpus/shebang.txt b/tree-sitter-nix-shell/corpus/shebang.txt similarity index 100% rename from corpus/shebang.txt rename to tree-sitter-nix-shell/corpus/shebang.txt diff --git a/grammar.js b/tree-sitter-nix-shell/grammar.js similarity index 100% rename from grammar.js rename to tree-sitter-nix-shell/grammar.js diff --git a/package.json b/tree-sitter-nix-shell/package.json similarity index 93% rename from package.json rename to tree-sitter-nix-shell/package.json index 57c8b59..bc7948e 100644 --- a/package.json +++ b/tree-sitter-nix-shell/package.json @@ -22,6 +22,7 @@ "content-regex": "^\b$nix-shell wraps other languages so intentionally fail against (and yield to) those other language matches", "injection-regex": "^(nix[-_]?shell)$", "scope": "source.nix_shell", + "injections": [ "queries/injections.scm" ], "highlights": [ "queries/highlights.scm" ] } ] diff --git a/tree-sitter-nix-shell/queries/highlights.scm b/tree-sitter-nix-shell/queries/highlights.scm new file mode 100644 index 0000000..fb68c60 --- /dev/null +++ b/tree-sitter-nix-shell/queries/highlights.scm @@ -0,0 +1,4 @@ +; these highlights will be overriden by the highlighting of the injected language. +; they might briefly be visible if the nix-shell directive is incomplete +; or if the embedded language doesn't support highlighting. +(nix_shell_directive) @keyword diff --git a/queries/injections.scm b/tree-sitter-nix-shell/queries/injections.scm similarity index 100% rename from queries/injections.scm rename to tree-sitter-nix-shell/queries/injections.scm