|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package sources |
| 5 | + |
| 6 | +import ( |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/components/components_testutils" |
| 11 | + "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" |
| 12 | + "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms" |
| 13 | + "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils" |
| 14 | + "github.com/spf13/afero" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "go.uber.org/mock/gomock" |
| 18 | +) |
| 19 | + |
| 20 | +const testSourcesDir = "/sources" |
| 21 | + |
| 22 | +func newTestPreparer(memFS afero.Fs) *sourcePreparerImpl { |
| 23 | + return &sourcePreparerImpl{ |
| 24 | + fs: memFS, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func writeTestSpec(t *testing.T, memFS afero.Fs, name, release string) { |
| 29 | + t.Helper() |
| 30 | + |
| 31 | + specDir := filepath.Join(testSourcesDir, name) |
| 32 | + require.NoError(t, fileutils.MkdirAll(memFS, specDir)) |
| 33 | + |
| 34 | + specPath := filepath.Join(specDir, name+".spec") |
| 35 | + content := []byte("Name: " + name + "\nVersion: 1.0.0\nRelease: " + release + "\nSummary: Test\nLicense: MIT\n") |
| 36 | + |
| 37 | + require.NoError(t, fileutils.WriteFile(memFS, specPath, content, fileperms.PublicFile)) |
| 38 | +} |
| 39 | + |
| 40 | +func mockComponent( |
| 41 | + ctrl *gomock.Controller, name string, config *projectconfig.ComponentConfig, |
| 42 | +) *components_testutils.MockComponent { |
| 43 | + comp := components_testutils.NewMockComponent(ctrl) |
| 44 | + comp.EXPECT().GetName().AnyTimes().Return(name) |
| 45 | + comp.EXPECT().GetConfig().AnyTimes().Return(config) |
| 46 | + |
| 47 | + return comp |
| 48 | +} |
| 49 | + |
| 50 | +func TestTryBumpStaticRelease_ManualSkips(t *testing.T) { |
| 51 | + ctrl := gomock.NewController(t) |
| 52 | + memFS := afero.NewMemMapFs() |
| 53 | + preparer := newTestPreparer(memFS) |
| 54 | + |
| 55 | + comp := mockComponent(ctrl, "kernel", &projectconfig.ComponentConfig{ |
| 56 | + ReleaseCalculation: projectconfig.ReleaseCalculationManual, |
| 57 | + }) |
| 58 | + |
| 59 | + // No spec file needed — should skip before reading anything. |
| 60 | + err := preparer.tryBumpStaticRelease(comp, testSourcesDir, 3) |
| 61 | + require.NoError(t, err) |
| 62 | +} |
| 63 | + |
| 64 | +func TestTryBumpStaticRelease_AutoreleaseSkips(t *testing.T) { |
| 65 | + ctrl := gomock.NewController(t) |
| 66 | + memFS := afero.NewMemMapFs() |
| 67 | + preparer := newTestPreparer(memFS) |
| 68 | + |
| 69 | + writeTestSpec(t, memFS, "test-pkg", "%autorelease") |
| 70 | + |
| 71 | + comp := mockComponent(ctrl, "test-pkg", &projectconfig.ComponentConfig{ |
| 72 | + ReleaseCalculation: projectconfig.ReleaseCalculationAuto, |
| 73 | + }) |
| 74 | + |
| 75 | + err := preparer.tryBumpStaticRelease(comp, filepath.Join(testSourcesDir, "test-pkg"), 3) |
| 76 | + require.NoError(t, err) |
| 77 | +} |
| 78 | + |
| 79 | +func TestTryBumpStaticRelease_StaticBumps(t *testing.T) { |
| 80 | + ctrl := gomock.NewController(t) |
| 81 | + memFS := afero.NewMemMapFs() |
| 82 | + preparer := newTestPreparer(memFS) |
| 83 | + |
| 84 | + writeTestSpec(t, memFS, "test-pkg", "1%{?dist}") |
| 85 | + |
| 86 | + comp := mockComponent(ctrl, "test-pkg", &projectconfig.ComponentConfig{ |
| 87 | + ReleaseCalculation: projectconfig.ReleaseCalculationAuto, |
| 88 | + }) |
| 89 | + |
| 90 | + err := preparer.tryBumpStaticRelease(comp, filepath.Join(testSourcesDir, "test-pkg"), 3) |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + // Verify the spec was updated. |
| 94 | + specPath := filepath.Join(testSourcesDir, "test-pkg", "test-pkg.spec") |
| 95 | + content, err := fileutils.ReadFile(memFS, specPath) |
| 96 | + require.NoError(t, err) |
| 97 | + assert.Contains(t, string(content), "Release: 4%{?dist}") |
| 98 | +} |
| 99 | + |
| 100 | +func TestTryBumpStaticRelease_NonStandardErrorsWithoutManual(t *testing.T) { |
| 101 | + ctrl := gomock.NewController(t) |
| 102 | + memFS := afero.NewMemMapFs() |
| 103 | + preparer := newTestPreparer(memFS) |
| 104 | + |
| 105 | + writeTestSpec(t, memFS, "kernel", "%{pkg_release}") |
| 106 | + |
| 107 | + comp := mockComponent(ctrl, "kernel", &projectconfig.ComponentConfig{ |
| 108 | + ReleaseCalculation: projectconfig.ReleaseCalculationAuto, |
| 109 | + }) |
| 110 | + |
| 111 | + err := preparer.tryBumpStaticRelease(comp, filepath.Join(testSourcesDir, "kernel"), 3) |
| 112 | + require.Error(t, err) |
| 113 | + assert.Contains(t, err.Error(), "cannot be auto-bumped") |
| 114 | + assert.Contains(t, err.Error(), "release-calculation") |
| 115 | +} |
| 116 | + |
| 117 | +func TestTryBumpStaticRelease_NonStandardSucceedsWithManual(t *testing.T) { |
| 118 | + ctrl := gomock.NewController(t) |
| 119 | + memFS := afero.NewMemMapFs() |
| 120 | + preparer := newTestPreparer(memFS) |
| 121 | + |
| 122 | + writeTestSpec(t, memFS, "kernel", "%{pkg_release}") |
| 123 | + |
| 124 | + comp := mockComponent(ctrl, "kernel", &projectconfig.ComponentConfig{ |
| 125 | + ReleaseCalculation: projectconfig.ReleaseCalculationManual, |
| 126 | + }) |
| 127 | + |
| 128 | + err := preparer.tryBumpStaticRelease(comp, filepath.Join(testSourcesDir, "kernel"), 3) |
| 129 | + require.NoError(t, err) |
| 130 | +} |
0 commit comments