|
| 1 | +// Copyright 2020 OpenSSF Scorecard Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gitlab |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/golang/mock/gomock" |
| 22 | + |
| 23 | + "github.com/ossf/scorecard/v4/checker" |
| 24 | + mockrepo "github.com/ossf/scorecard/v4/clients/mockclients" |
| 25 | +) |
| 26 | + |
| 27 | +func TestGitlabPackagingYamlCheck(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + //nolint |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + lineNumber uint |
| 34 | + filename string |
| 35 | + exists bool |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "No Publishing Detected", |
| 39 | + filename: "./testdata/no-publishing.yaml", |
| 40 | + lineNumber: 1, |
| 41 | + exists: false, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Docker", |
| 45 | + filename: "./testdata/docker.yaml", |
| 46 | + lineNumber: 31, |
| 47 | + exists: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Nuget", |
| 51 | + filename: "./testdata/nuget.yaml", |
| 52 | + lineNumber: 21, |
| 53 | + exists: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Poetry", |
| 57 | + filename: "./testdata/poetry.yaml", |
| 58 | + lineNumber: 30, |
| 59 | + exists: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "Twine", |
| 63 | + filename: "./testdata/twine.yaml", |
| 64 | + lineNumber: 26, |
| 65 | + exists: true, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for _, tt := range tests { |
| 70 | + tt := tt |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + var content []byte |
| 74 | + var err error |
| 75 | + |
| 76 | + content, err = os.ReadFile(tt.filename) |
| 77 | + if err != nil { |
| 78 | + t.Errorf("cannot read file: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + file, found := isGitlabPackagingWorkflow(content, tt.filename) |
| 82 | + |
| 83 | + if tt.exists && !found { |
| 84 | + t.Errorf("Packaging %q should exist", tt.name) |
| 85 | + } else if !tt.exists && found { |
| 86 | + t.Errorf("No packaging information should have been found in %q", tt.name) |
| 87 | + } |
| 88 | + |
| 89 | + if file.Offset != tt.lineNumber { |
| 90 | + t.Errorf("Expected line number: %d != %d", tt.lineNumber, file.Offset) |
| 91 | + } |
| 92 | + |
| 93 | + if err != nil { |
| 94 | + return |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestGitlabPackagingPackager(t *testing.T) { |
| 101 | + t.Parallel() |
| 102 | + |
| 103 | + //nolint |
| 104 | + tests := []struct { |
| 105 | + name string |
| 106 | + lineNumber uint |
| 107 | + filename string |
| 108 | + exists bool |
| 109 | + }{ |
| 110 | + { |
| 111 | + name: "No Publishing Detected", |
| 112 | + filename: "./testdata/no-publishing.yaml", |
| 113 | + lineNumber: 1, |
| 114 | + exists: false, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "Docker", |
| 118 | + filename: "./testdata/docker.yaml", |
| 119 | + lineNumber: 31, |
| 120 | + exists: true, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + tt := tt |
| 126 | + t.Run(tt.name, func(t *testing.T) { |
| 127 | + t.Parallel() |
| 128 | + |
| 129 | + ctrl := gomock.NewController(t) |
| 130 | + defer ctrl.Finish() |
| 131 | + moqRepoClient := mockrepo.NewMockRepoClient(ctrl) |
| 132 | + moqRepo := mockrepo.NewMockRepo(ctrl) |
| 133 | + |
| 134 | + moqRepoClient.EXPECT().ListFiles(gomock.Any()). |
| 135 | + Return([]string{tt.filename}, nil).AnyTimes() |
| 136 | + |
| 137 | + moqRepoClient.EXPECT().GetFileContent(tt.filename). |
| 138 | + DoAndReturn(func(b string) ([]byte, error) { |
| 139 | + //nolint: errcheck |
| 140 | + content, _ := os.ReadFile(b) |
| 141 | + return content, nil |
| 142 | + }).AnyTimes() |
| 143 | + |
| 144 | + if tt.exists { |
| 145 | + moqRepo.EXPECT().URI().Return("myurl.com/owner/project") |
| 146 | + } |
| 147 | + |
| 148 | + req := checker.CheckRequest{ |
| 149 | + RepoClient: moqRepoClient, |
| 150 | + Repo: moqRepo, |
| 151 | + } |
| 152 | + |
| 153 | + //nolint: errcheck |
| 154 | + packagingData, _ := Packaging(&req) |
| 155 | + |
| 156 | + if !tt.exists { |
| 157 | + if len(packagingData.Packages) != 0 { |
| 158 | + t.Errorf("Repo should not contain any packages") |
| 159 | + } |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + if len(packagingData.Packages) == 0 { |
| 164 | + t.Fatalf("Repo should contain related packages") |
| 165 | + } |
| 166 | + |
| 167 | + pkg := packagingData.Packages[0].File |
| 168 | + |
| 169 | + if pkg.Offset != tt.lineNumber { |
| 170 | + t.Errorf("Expected line number: %d != %d", tt.lineNumber, pkg.Offset) |
| 171 | + } |
| 172 | + if pkg.Path != tt.filename { |
| 173 | + t.Errorf("Expected filename: %v != %v", tt.filename, pkg.Path) |
| 174 | + } |
| 175 | + |
| 176 | + runs := packagingData.Packages[0].Runs |
| 177 | + |
| 178 | + if len(runs) != 1 { |
| 179 | + t.Errorf("Expected only a single run count, but received %d", len(runs)) |
| 180 | + } |
| 181 | + |
| 182 | + if runs[0].URL != "myurl.com/owner/project" { |
| 183 | + t.Errorf("URL did not match expected value %q", runs[0].URL) |
| 184 | + } |
| 185 | + }) |
| 186 | + } |
| 187 | +} |
0 commit comments