|
1 | 1 | package linter |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "os" |
5 | 6 | "path/filepath" |
6 | 7 | "strings" |
@@ -842,3 +843,123 @@ jobs: |
842 | 843 | t.Error("Expected to find 'Job name too short' issue") |
843 | 844 | } |
844 | 845 | } |
| 846 | + |
| 847 | +func TestStyleLinter_RunScriptLength(t *testing.T) { |
| 848 | + tests := []struct { |
| 849 | + name string |
| 850 | + maxLines int |
| 851 | + runScript string |
| 852 | + wantFlagged bool |
| 853 | + }{ |
| 854 | + { |
| 855 | + name: "too long", |
| 856 | + maxLines: 5, |
| 857 | + runScript: "echo 1\necho 2\necho 3\necho 4\necho 5\necho 6", |
| 858 | + wantFlagged: true, |
| 859 | + }, |
| 860 | + { |
| 861 | + name: "within limit", |
| 862 | + maxLines: 5, |
| 863 | + runScript: "echo 1\necho 2\necho 3", |
| 864 | + wantFlagged: false, |
| 865 | + }, |
| 866 | + { |
| 867 | + name: "check disabled", |
| 868 | + maxLines: 0, |
| 869 | + runScript: "echo 1\necho 2\necho 3\necho 4\necho 5\necho 6\necho 7\necho 8\necho 9\necho 10", |
| 870 | + wantFlagged: false, |
| 871 | + }, |
| 872 | + { |
| 873 | + name: "single line", |
| 874 | + maxLines: 1, |
| 875 | + runScript: "echo hello", |
| 876 | + wantFlagged: false, |
| 877 | + }, |
| 878 | + { |
| 879 | + name: "exact boundary", |
| 880 | + maxLines: 5, |
| 881 | + runScript: "echo 1\necho 2\necho 3\necho 4\necho 5", |
| 882 | + wantFlagged: false, |
| 883 | + }, |
| 884 | + } |
| 885 | + |
| 886 | + for _, tt := range tests { |
| 887 | + t.Run(tt.name, func(t *testing.T) { |
| 888 | + tmpDir := t.TempDir() |
| 889 | + workflowPath := filepath.Join(tmpDir, "test.yml") |
| 890 | + |
| 891 | + content := fmt.Sprintf(`name: Test Workflow |
| 892 | +on: push |
| 893 | +jobs: |
| 894 | + build: |
| 895 | + runs-on: ubuntu-latest |
| 896 | + steps: |
| 897 | + - name: Test Step |
| 898 | + run: | |
| 899 | + %s |
| 900 | +`, strings.ReplaceAll(tt.runScript, "\n", "\n ")) |
| 901 | + |
| 902 | + if err := os.WriteFile(workflowPath, []byte(content), 0600); err != nil { |
| 903 | + t.Fatalf("Failed to write test workflow: %v", err) |
| 904 | + } |
| 905 | + |
| 906 | + wf, err := workflow.LoadWorkflow(workflowPath) |
| 907 | + if err != nil { |
| 908 | + t.Fatalf("LoadWorkflow() error = %v", err) |
| 909 | + } |
| 910 | + |
| 911 | + linter := NewStyleLinter(&config.StyleSettings{MaxRunLines: tt.maxLines}) |
| 912 | + issues, err := linter.LintWorkflow(wf) |
| 913 | + if err != nil { |
| 914 | + t.Fatalf("LintWorkflow() error = %v", err) |
| 915 | + } |
| 916 | + |
| 917 | + found := false |
| 918 | + for _, issue := range issues { |
| 919 | + if strings.Contains(issue.Message, "Run script has") { |
| 920 | + found = true |
| 921 | + break |
| 922 | + } |
| 923 | + } |
| 924 | + |
| 925 | + if found != tt.wantFlagged { |
| 926 | + t.Errorf("got flagged=%v, want flagged=%v", found, tt.wantFlagged) |
| 927 | + } |
| 928 | + }) |
| 929 | + } |
| 930 | +} |
| 931 | + |
| 932 | +func TestStyleLinter_RunScriptStepWithUsesOnly(t *testing.T) { |
| 933 | + tmpDir := t.TempDir() |
| 934 | + workflowPath := filepath.Join(tmpDir, "test.yml") |
| 935 | + |
| 936 | + content := `name: Test Workflow |
| 937 | +on: push |
| 938 | +jobs: |
| 939 | + build: |
| 940 | + runs-on: ubuntu-latest |
| 941 | + steps: |
| 942 | + - name: Checkout |
| 943 | + uses: actions/checkout@v4 |
| 944 | +` |
| 945 | + if err := os.WriteFile(workflowPath, []byte(content), 0600); err != nil { |
| 946 | + t.Fatalf("Failed to write test workflow: %v", err) |
| 947 | + } |
| 948 | + |
| 949 | + wf, err := workflow.LoadWorkflow(workflowPath) |
| 950 | + if err != nil { |
| 951 | + t.Fatalf("LoadWorkflow() error = %v", err) |
| 952 | + } |
| 953 | + |
| 954 | + linter := NewStyleLinter(&config.StyleSettings{MaxRunLines: 1}) |
| 955 | + issues, err := linter.LintWorkflow(wf) |
| 956 | + if err != nil { |
| 957 | + t.Fatalf("LintWorkflow() error = %v", err) |
| 958 | + } |
| 959 | + |
| 960 | + for _, issue := range issues { |
| 961 | + if strings.Contains(issue.Message, "Run script has") { |
| 962 | + t.Error("Did not expect 'uses' step to trigger run script check") |
| 963 | + } |
| 964 | + } |
| 965 | +} |
0 commit comments