|
| 1 | +/* |
| 2 | +Copyright 2026 The Radius Authors. |
| 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 terraform |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "runtime" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +// TestTerraformVersionMatchesFile guarantees that the hard-coded |
| 30 | +// terraformVersion default in version.go stays in sync with the |
| 31 | +// .terraform-version file at the repository root. |
| 32 | +func TestTerraformVersionMatchesFile(t *testing.T) { |
| 33 | + _, thisFile, _, ok := runtime.Caller(0) |
| 34 | + require.True(t, ok, "unable to determine test file location") |
| 35 | + |
| 36 | + // Walk up from this file to the repo root (which contains go.mod). |
| 37 | + dir := filepath.Dir(thisFile) |
| 38 | + for { |
| 39 | + if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { |
| 40 | + break |
| 41 | + } |
| 42 | + parent := filepath.Dir(dir) |
| 43 | + require.NotEqual(t, parent, dir, "could not locate repository root (go.mod)") |
| 44 | + dir = parent |
| 45 | + } |
| 46 | + |
| 47 | + contents, err := os.ReadFile(filepath.Join(dir, ".terraform-version")) |
| 48 | + require.NoError(t, err, "failed to read .terraform-version at repo root") |
| 49 | + |
| 50 | + require.Equal(t, |
| 51 | + strings.TrimSpace(string(contents)), |
| 52 | + terraformVersion, |
| 53 | + "terraformVersion default in version.go must match .terraform-version at repo root", |
| 54 | + ) |
| 55 | +} |
0 commit comments