|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package lockfile_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/microsoft/azure-linux-dev-tools/internal/lockfile" |
| 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 | +) |
| 18 | + |
| 19 | +const testProjectDir = "/project" |
| 20 | + |
| 21 | +func TestNew(t *testing.T) { |
| 22 | + lf := lockfile.New() |
| 23 | + assert.Equal(t, 1, lf.Version) |
| 24 | + assert.NotNil(t, lf.Components) |
| 25 | + assert.Empty(t, lf.Components) |
| 26 | +} |
| 27 | + |
| 28 | +func TestSetAndGetUpstreamCommit(t *testing.T) { |
| 29 | + lf := lockfile.New() |
| 30 | + |
| 31 | + lf.SetUpstreamCommit("curl", "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2") |
| 32 | + |
| 33 | + commit, ok := lf.GetUpstreamCommit("curl") |
| 34 | + assert.True(t, ok) |
| 35 | + assert.Equal(t, "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2", commit) |
| 36 | +} |
| 37 | + |
| 38 | +func TestGetUpstreamCommitMissing(t *testing.T) { |
| 39 | + lf := lockfile.New() |
| 40 | + |
| 41 | + commit, ok := lf.GetUpstreamCommit("nonexistent") |
| 42 | + assert.False(t, ok) |
| 43 | + assert.Empty(t, commit) |
| 44 | +} |
| 45 | + |
| 46 | +func TestSaveAndLoad(t *testing.T) { |
| 47 | + memFS := afero.NewMemMapFs() |
| 48 | + lockPath := filepath.Join(testProjectDir, lockfile.FileName) |
| 49 | + |
| 50 | + require.NoError(t, fileutils.MkdirAll(memFS, testProjectDir)) |
| 51 | + |
| 52 | + // Create and save a lock file. |
| 53 | + original := lockfile.New() |
| 54 | + original.SetUpstreamCommit("curl", "aaaa") |
| 55 | + original.SetUpstreamCommit("bash", "bbbb") |
| 56 | + original.SetUpstreamCommit("vim", "cccc") |
| 57 | + |
| 58 | + require.NoError(t, original.Save(memFS, lockPath)) |
| 59 | + |
| 60 | + // Load it back. |
| 61 | + loaded, err := lockfile.Load(memFS, lockPath) |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + assert.Equal(t, 1, loaded.Version) |
| 65 | + |
| 66 | + commit, found := loaded.GetUpstreamCommit("curl") |
| 67 | + assert.True(t, found) |
| 68 | + assert.Equal(t, "aaaa", commit) |
| 69 | + |
| 70 | + commit, found = loaded.GetUpstreamCommit("bash") |
| 71 | + assert.True(t, found) |
| 72 | + assert.Equal(t, "bbbb", commit) |
| 73 | + |
| 74 | + commit, found = loaded.GetUpstreamCommit("vim") |
| 75 | + assert.True(t, found) |
| 76 | + assert.Equal(t, "cccc", commit) |
| 77 | +} |
| 78 | + |
| 79 | +func TestSaveSortsComponents(t *testing.T) { |
| 80 | + memFS := afero.NewMemMapFs() |
| 81 | + lockPath := filepath.Join(testProjectDir, lockfile.FileName) |
| 82 | + |
| 83 | + require.NoError(t, fileutils.MkdirAll(memFS, testProjectDir)) |
| 84 | + |
| 85 | + lockFile := lockfile.New() |
| 86 | + // Insert in non-alphabetical order. |
| 87 | + lockFile.SetUpstreamCommit("zlib", "zzzz") |
| 88 | + lockFile.SetUpstreamCommit("curl", "aaaa") |
| 89 | + lockFile.SetUpstreamCommit("bash", "bbbb") |
| 90 | + |
| 91 | + require.NoError(t, lockFile.Save(memFS, lockPath)) |
| 92 | + |
| 93 | + data, err := fileutils.ReadFile(memFS, lockPath) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + content := string(data) |
| 97 | + |
| 98 | + // bash should appear before curl, which should appear before zlib. |
| 99 | + bashIdx := strings.Index(content, "[components.bash]") |
| 100 | + curlIdx := strings.Index(content, "[components.curl]") |
| 101 | + zlibIdx := strings.Index(content, "[components.zlib]") |
| 102 | + |
| 103 | + assert.Less(t, bashIdx, curlIdx, "bash should come before curl") |
| 104 | + assert.Less(t, curlIdx, zlibIdx, "curl should come before zlib") |
| 105 | +} |
| 106 | + |
| 107 | +func TestLoadUnsupportedVersion(t *testing.T) { |
| 108 | + memFS := afero.NewMemMapFs() |
| 109 | + lockPath := filepath.Join(testProjectDir, lockfile.FileName) |
| 110 | + |
| 111 | + content := "version = 99\n" |
| 112 | + |
| 113 | + require.NoError(t, fileutils.MkdirAll(memFS, testProjectDir)) |
| 114 | + require.NoError(t, fileutils.WriteFile(memFS, lockPath, []byte(content), fileperms.PublicFile)) |
| 115 | + |
| 116 | + _, err := lockfile.Load(memFS, lockPath) |
| 117 | + assert.ErrorContains(t, err, "unsupported lock file version") |
| 118 | +} |
| 119 | + |
| 120 | +func TestLoadMissingFile(t *testing.T) { |
| 121 | + fs := afero.NewMemMapFs() |
| 122 | + |
| 123 | + _, err := lockfile.Load(fs, "/nonexistent/azldev.lock") |
| 124 | + assert.Error(t, err) |
| 125 | +} |
| 126 | + |
| 127 | +func TestLoadInvalidTOML(t *testing.T) { |
| 128 | + memFS := afero.NewMemMapFs() |
| 129 | + lockPath := filepath.Join(testProjectDir, lockfile.FileName) |
| 130 | + |
| 131 | + require.NoError(t, fileutils.MkdirAll(memFS, testProjectDir)) |
| 132 | + require.NoError(t, fileutils.WriteFile(memFS, lockPath, []byte("not valid toml {{{"), fileperms.PublicFile)) |
| 133 | + |
| 134 | + _, err := lockfile.Load(memFS, lockPath) |
| 135 | + assert.ErrorContains(t, err, "parsing lock file") |
| 136 | +} |
| 137 | + |
| 138 | +func TestSaveContainsVersion(t *testing.T) { |
| 139 | + memFS := afero.NewMemMapFs() |
| 140 | + lockPath := filepath.Join(testProjectDir, lockfile.FileName) |
| 141 | + |
| 142 | + require.NoError(t, fileutils.MkdirAll(memFS, testProjectDir)) |
| 143 | + |
| 144 | + lockFile := lockfile.New() |
| 145 | + require.NoError(t, lockFile.Save(memFS, lockPath)) |
| 146 | + |
| 147 | + data, err := fileutils.ReadFile(memFS, lockPath) |
| 148 | + require.NoError(t, err) |
| 149 | + |
| 150 | + assert.Contains(t, string(data), "version = 1") |
| 151 | + assert.Contains(t, string(data), "# azldev.lock") |
| 152 | +} |
0 commit comments