|
| 1 | +/* |
| 2 | +Copyright 2025 Flant JSC |
| 3 | + |
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | + |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package rules |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + |
| 27 | + "github.com/deckhouse/dmt/pkg/errors" |
| 28 | +) |
| 29 | + |
| 30 | +func TestChangelogRule_CheckChangelog(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + fileContent string |
| 34 | + createFile bool |
| 35 | + expectErrors int |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "changelog.yaml file missing", |
| 39 | + createFile: false, |
| 40 | + expectErrors: 1, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "changelog.yaml file empty", |
| 44 | + fileContent: "", |
| 45 | + createFile: true, |
| 46 | + expectErrors: 1, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "changelog.yaml file present and non-empty", |
| 50 | + fileContent: "# Changelog\n\n## v1.0.0\n- Initial release", |
| 51 | + createFile: true, |
| 52 | + expectErrors: 0, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + tempDir := t.TempDir() |
| 59 | + if tt.createFile { |
| 60 | + err := os.WriteFile(filepath.Join(tempDir, changelogFilename), []byte(tt.fileContent), 0600) |
| 61 | + require.NoError(t, err) |
| 62 | + } |
| 63 | + |
| 64 | + rule := NewChangelogRule() |
| 65 | + errorList := errors.NewLintRuleErrorsList() |
| 66 | + |
| 67 | + rule.CheckChangelog(tempDir, errorList) |
| 68 | + |
| 69 | + if tt.expectErrors > 0 { |
| 70 | + assert.True(t, errorList.ContainsErrors(), "Expected errors but got none") |
| 71 | + assert.Len(t, errorList.GetErrors(), tt.expectErrors) |
| 72 | + } else { |
| 73 | + assert.False(t, errorList.ContainsErrors(), "Expected no errors but got some") |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
0 commit comments