|
7 | 7 | lib.optionalString
|
8 | 8 | (settings.rust.cargoManifestPath != null)
|
9 | 9 | "--manifest-path ${lib.escapeShellArg settings.rust.cargoManifestPath}";
|
| 10 | + |
| 11 | + mkCmdArgs = predActionList: |
| 12 | + lib.concatStringsSep |
| 13 | + " " |
| 14 | + (builtins.foldl' |
| 15 | + (acc: entry: |
| 16 | + acc ++ lib.optional (builtins.elemAt entry 0) (builtins.elemAt entry 1)) |
| 17 | + [ ] |
| 18 | + predActionList); |
| 19 | + |
10 | 20 | in
|
11 | 21 | {
|
12 | 22 | options.settings =
|
|
761 | 771 | files = "\\.go$";
|
762 | 772 | };
|
763 | 773 |
|
| 774 | + gotest = { |
| 775 | + enable = true; |
| 776 | + name = "gotest"; |
| 777 | + description = "Run go tests"; |
| 778 | + entry = |
| 779 | + let |
| 780 | + script = pkgs.writeShellScript "precommit-gotest" '' |
| 781 | + set -e |
| 782 | + # find all directories that contain tests |
| 783 | + dirs=() |
| 784 | + for file in "$@"; do |
| 785 | + # either the file is a test |
| 786 | + if [[ "$file" = *_test.go ]]; then |
| 787 | + dirs+=("$(dirname "$file")") |
| 788 | + continue |
| 789 | + fi |
| 790 | +
|
| 791 | + # or the file has an associated test |
| 792 | + filename="''${file%.go}" |
| 793 | + test_file="''${filename}_test.go" |
| 794 | + if [[ -f "$test_file" ]]; then |
| 795 | + dirs+=("$(dirname "$test_file")") |
| 796 | + continue |
| 797 | + fi |
| 798 | + done |
| 799 | +
|
| 800 | + # ensure we are not duplicating dir entries |
| 801 | + IFS=$'\n' sorted_dirs=($(sort -u <<<"''${dirs[*]}")); unset IFS |
| 802 | +
|
| 803 | + # test each directory one by one |
| 804 | + for dir in "''${sorted_dirs[@]}"; do |
| 805 | + ${tools.go}/bin/go test "./$dir" |
| 806 | + done |
| 807 | + ''; |
| 808 | + in |
| 809 | + builtins.toString script; |
| 810 | + files = "\\.go$"; |
| 811 | + raw = { |
| 812 | + # to avoid multiple invocations of the same directory input, provide |
| 813 | + # all file names in a single run. |
| 814 | + require_serial = true; |
| 815 | + }; |
| 816 | + }; |
| 817 | + |
| 818 | + gofmt = |
| 819 | + { |
| 820 | + name = "gofmt"; |
| 821 | + description = "A tool that automatically formats Go source code"; |
| 822 | + entry = |
| 823 | + let |
| 824 | + script = pkgs.writeShellScript "precommit-gofmt" '' |
| 825 | + set -e |
| 826 | + failed=false |
| 827 | + for file in "$@"; do |
| 828 | + # redirect stderr so that violations and summaries are properly interleaved. |
| 829 | + if ! ${tools.go}/bin/gofmt -l -w "$file" 2>&1 |
| 830 | + then |
| 831 | + failed=true |
| 832 | + fi |
| 833 | + done |
| 834 | + if [[ $failed == "true" ]]; then |
| 835 | + exit 1 |
| 836 | + fi |
| 837 | + ''; |
| 838 | + in |
| 839 | + builtins.toString script; |
| 840 | + files = "\\.go$"; |
| 841 | + }; |
| 842 | + |
764 | 843 | revive =
|
765 | 844 | {
|
766 | 845 | name = "revive";
|
767 | 846 | description = "A linter for Go source code.";
|
768 | 847 | entry =
|
769 | 848 | let
|
770 | 849 | cmdArgs =
|
771 |
| - lib.concatStringsSep |
772 |
| - " " |
773 |
| - (builtins.concatLists [ |
774 |
| - [ "-set_exit_status" ] |
775 |
| - (if settings.revive.configPath != "" then |
776 |
| - [ "-config ${settings.revive.configPath}" ] |
777 |
| - else |
778 |
| - [ ]) |
779 |
| - ] |
780 |
| - ); |
| 850 | + mkCmdArgs [ |
| 851 | + [ true "-set_exit_status" ] |
| 852 | + [ (settings.revive.configPath != "") "-config ${settings.revive.configPath}" ] |
| 853 | + ]; |
781 | 854 | # revive works with both files and directories; however some lints
|
782 | 855 | # may fail (e.g. package-comment) if they run on an individual file
|
783 | 856 | # rather than a package/directory scope; given this let's get the
|
|
798 | 871 | };
|
799 | 872 | };
|
800 | 873 |
|
| 874 | + staticcheck = |
| 875 | + { |
| 876 | + enable = true; |
| 877 | + name = "staticcheck"; |
| 878 | + description = "State of the art linter for the Go programming language"; |
| 879 | + # staticheck works with directories. |
| 880 | + entry = |
| 881 | + let |
| 882 | + script = pkgs.writeShellScript "precommit-staticcheck" '' |
| 883 | + err=0 |
| 884 | + for dir in $(echo "$@" | xargs -n1 dirname | sort -u); do |
| 885 | + ${tools.go-tools}/bin/staticcheck ./"$dir" |
| 886 | + code="$?" |
| 887 | + if [[ "$err" -eq 0 ]]; then |
| 888 | + err="$code" |
| 889 | + fi |
| 890 | + done |
| 891 | + exit $err |
| 892 | + ''; |
| 893 | + in |
| 894 | + builtins.toString script; |
| 895 | + files = "\\.go$"; |
| 896 | + raw = { |
| 897 | + # to avoid multiple invocations of the same directory input, provide |
| 898 | + # all file names in a single run. |
| 899 | + require_serial = true; |
| 900 | + }; |
| 901 | + }; |
| 902 | + |
801 | 903 | editorconfig-checker =
|
802 | 904 | {
|
803 | 905 | name = "editorconfig-checker";
|
|
0 commit comments