(t *testing.T)
| 178 | } |
| 179 | |
| 180 | func TestRequiresUserCapabilityForScript(t *testing.T) { |
| 181 | tests := []struct { |
| 182 | name string |
| 183 | content string |
| 184 | want bool |
| 185 | wantErr string |
| 186 | }{ |
| 187 | { |
| 188 | name: "requires user capability", |
| 189 | content: "# requires-user-capability: true\n", |
| 190 | want: true, |
| 191 | }, |
| 192 | { |
| 193 | name: "does not require user capability", |
| 194 | content: "# requires-user-capability: false\n", |
| 195 | }, |
| 196 | { |
| 197 | name: "supports CRLF", |
| 198 | content: "# requires-user-capability: true\r\n", |
| 199 | want: true, |
| 200 | }, |
| 201 | { |
| 202 | name: "missing", |
| 203 | content: "", |
| 204 | wantErr: "first line must be", |
| 205 | }, |
| 206 | { |
| 207 | name: "not first", |
| 208 | content: "# explanation\n# requires-user-capability: true\n", |
| 209 | wantErr: "first line must be", |
| 210 | }, |
| 211 | { |
| 212 | name: "invalid value", |
| 213 | content: "# requires-user-capability: unknown\n", |
| 214 | wantErr: "first line must be", |
| 215 | }, |
| 216 | { |
| 217 | name: "multiple", |
| 218 | content: "# requires-user-capability: false\n# requires-user-capability: true\n", |
| 219 | wantErr: "script must contain exactly one requires-user-capability declaration", |
| 220 | }, |
| 221 | { |
| 222 | name: "ignores archive contents", |
| 223 | content: "# requires-user-capability: false\n\n" + |
| 224 | "-- script.sh --\n" + |
| 225 | "# requires-user-capability: true\n", |
| 226 | }, |
| 227 | } |
| 228 | |
| 229 | for _, tt := range tests { |
| 230 | t.Run(tt.name, func(t *testing.T) { |
| 231 | file := filepath.Join(t.TempDir(), "script.txtar") |
| 232 | require.NoError(t, os.WriteFile(file, []byte(tt.content), 0o600)) |
| 233 | |
| 234 | got, err := requiresUserCapabilityForScript(file) |
| 235 | if tt.wantErr == "" { |
| 236 | require.NoError(t, err) |
| 237 | assert.Equal(t, tt.want, got) |
nothing calls this directly
no test coverage detected