|
761 | 761 | files = "\\.go$";
|
762 | 762 | };
|
763 | 763 |
|
| 764 | + gotest = { |
| 765 | + enable = true; |
| 766 | + name = "gotest"; |
| 767 | + description = "Run go tests"; |
| 768 | + entry = |
| 769 | + let |
| 770 | + script = pkgs.writeShellScript "precommit-gotest" '' |
| 771 | + set -e |
| 772 | + # find all directories that contain tests |
| 773 | + dirs=() |
| 774 | + for file in "$@"; do |
| 775 | + # either the file is a test |
| 776 | + if [[ "$file" = *_test.go ]]; then |
| 777 | + dirs+=("$(dirname "$file")") |
| 778 | + continue |
| 779 | + fi |
| 780 | +
|
| 781 | + # or the file has an associated test |
| 782 | + filename="''${file%.go}" |
| 783 | + test_file="''${filename}_test.go" |
| 784 | + if [[ -f "$test_file" ]]; then |
| 785 | + dirs+=("$(dirname "$test_file")") |
| 786 | + continue |
| 787 | + fi |
| 788 | + done |
| 789 | +
|
| 790 | + # ensure we are not duplicating dir entries |
| 791 | + IFS=$'\n' sorted_dirs=($(sort -u <<<"''${dirs[*]}")); unset IFS |
| 792 | +
|
| 793 | + # test each directory one by one |
| 794 | + for dir in "''${sorted_dirs[@]}"; do |
| 795 | + ${tools.go}/bin/go test "./$dir" |
| 796 | + done |
| 797 | + ''; |
| 798 | + in |
| 799 | + builtins.toString script; |
| 800 | + files = "\\.go$"; |
| 801 | + raw = { |
| 802 | + # to avoid multiple invocations of the same directory input, provide |
| 803 | + # all file names in a single run. |
| 804 | + require_serial = true; |
| 805 | + }; |
| 806 | + }; |
| 807 | + |
| 808 | + gofmt = |
| 809 | + { |
| 810 | + name = "gofmt"; |
| 811 | + description = "A tool that automatically formats Go source code"; |
| 812 | + entry = |
| 813 | + let |
| 814 | + script = pkgs.writeShellScript "precommit-gofmt" '' |
| 815 | + set -e |
| 816 | + failed=false |
| 817 | + for file in "$@"; do |
| 818 | + # redirect stderr so that violations and summaries are properly interleaved. |
| 819 | + if ! ${tools.go}/bin/gofmt -l -w "$file" 2>&1 |
| 820 | + then |
| 821 | + failed=true |
| 822 | + fi |
| 823 | + done |
| 824 | + if [[ $failed == "true" ]]; then |
| 825 | + exit 1 |
| 826 | + fi |
| 827 | + ''; |
| 828 | + in |
| 829 | + builtins.toString script; |
| 830 | + files = "\\.go$"; |
| 831 | + }; |
| 832 | + |
764 | 833 | revive =
|
765 | 834 | {
|
766 | 835 | name = "revive";
|
|
798 | 867 | };
|
799 | 868 | };
|
800 | 869 |
|
| 870 | + staticcheck = |
| 871 | + { |
| 872 | + enable = true; |
| 873 | + name = "staticcheck"; |
| 874 | + description = "State of the art linter for the Go programming language"; |
| 875 | + # staticheck works with directories. |
| 876 | + entry = |
| 877 | + let |
| 878 | + script = pkgs.writeShellScript "precommit-staticcheck" '' |
| 879 | + err=0 |
| 880 | + for dir in $(echo "$@" | xargs -n1 dirname | sort -u); do |
| 881 | + ${tools.go-tools}/bin/staticcheck ./"$dir" |
| 882 | + code="$?" |
| 883 | + if [[ "$err" -eq 0 ]]; then |
| 884 | + err="$code" |
| 885 | + fi |
| 886 | + done |
| 887 | + exit $err |
| 888 | + ''; |
| 889 | + in |
| 890 | + builtins.toString script; |
| 891 | + files = "\\.go$"; |
| 892 | + raw = { |
| 893 | + # to avoid multiple invocations of the same directory input, provide |
| 894 | + # all file names in a single run. |
| 895 | + require_serial = true; |
| 896 | + }; |
| 897 | + }; |
| 898 | + |
801 | 899 | editorconfig-checker =
|
802 | 900 | {
|
803 | 901 | name = "editorconfig-checker";
|
|
0 commit comments