kcl-lang.io/kpm@v0.8.7-0.20240520061008-9fc4c5efc8c7/pkg/package/modfile_test.go (about) 1 package pkg 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "kcl-lang.io/kpm/pkg/opt" 11 "kcl-lang.io/kpm/pkg/runner" 12 "kcl-lang.io/kpm/pkg/utils" 13 ) 14 15 func TestModFileWithDesc(t *testing.T) { 16 testPath := getTestDir("test_mod_with_desc") 17 isExist, err := ModFileExists(testPath) 18 assert.Equal(t, isExist, true) 19 assert.Equal(t, err, nil) 20 modFile, err := LoadModFile(testPath) 21 assert.Equal(t, modFile.Pkg.Name, "test_mod_with_desc") 22 assert.Equal(t, modFile.Pkg.Version, "0.0.1") 23 assert.Equal(t, modFile.Pkg.Edition, "0.0.1") 24 assert.Equal(t, modFile.Pkg.Description, "This is a test module with a description") 25 assert.Equal(t, len(modFile.Dependencies.Deps), 0) 26 assert.Equal(t, err, nil) 27 } 28 29 func TestWithTheSameVersion(t *testing.T) { 30 d := Dependency{ 31 Name: "test", 32 Version: "0.0.1", 33 } 34 35 d2 := Dependency{ 36 Name: "test", 37 Version: "0.0.2", 38 } 39 40 assert.Equal(t, d.WithTheSameVersion(d2), false) 41 42 d2.Version = "0.0.1" 43 assert.Equal(t, d.WithTheSameVersion(d2), true) 44 45 d2.Name = "test2" 46 assert.Equal(t, d.WithTheSameVersion(d2), false) 47 } 48 49 func TestModFileExists(t *testing.T) { 50 testDir := initTestDir("test_data_modfile") 51 // there is no 'kcl.mod' and 'kcl.mod.lock'. 52 is_exist, err := ModFileExists(testDir) 53 if err != nil || is_exist { 54 t.Errorf("test 'ModFileExists' failed.") 55 } 56 57 is_exist, err = ModLockFileExists(testDir) 58 if err != nil || is_exist { 59 t.Errorf("test 'ModLockFileExists' failed.") 60 } 61 62 modFile := NewModFile( 63 &opt.InitOptions{ 64 Name: "test_kcl_pkg", 65 InitPath: testDir, 66 }, 67 ) 68 // generate 'kcl.mod' but still no 'kcl.mod.lock'. 69 err = modFile.StoreModFile() 70 71 if err != nil { 72 t.Errorf("test 'Store' failed.") 73 } 74 75 is_exist, err = ModFileExists(testDir) 76 if err != nil || !is_exist { 77 t.Errorf("test 'Store' failed.") 78 } 79 80 is_exist, err = ModLockFileExists(testDir) 81 if err != nil || is_exist { 82 t.Errorf("test 'Store' failed.") 83 } 84 85 NewModFile, err := LoadModFile(testDir) 86 if err != nil || NewModFile.Pkg.Name != "test_kcl_pkg" || NewModFile.Pkg.Version != "0.0.1" || NewModFile.Pkg.Edition != runner.GetKclVersion() { 87 t.Errorf("test 'LoadModFile' failed.") 88 } 89 } 90 91 func TestParseOpt(t *testing.T) { 92 _, err := ParseOpt(&opt.RegistryOptions{ 93 Git: &opt.GitOptions{ 94 Url: "test.git", 95 Branch: "test_branch", 96 Commit: "test_commit", 97 Tag: "test_tag", 98 }, 99 }) 100 assert.Equal(t, err.Error(), "only one of branch, tag or commit is allowed") 101 102 dep, err := ParseOpt(&opt.RegistryOptions{ 103 Git: &opt.GitOptions{ 104 Url: "test.git", 105 Branch: "test_branch", 106 Commit: "", 107 Tag: "", 108 }, 109 }) 110 assert.Equal(t, err, nil) 111 assert.Equal(t, dep.Name, "test") 112 assert.Equal(t, dep.FullName, "test_test_branch") 113 assert.Equal(t, dep.Url, "test.git") 114 assert.Equal(t, dep.Branch, "test_branch") 115 assert.Equal(t, dep.Commit, "") 116 assert.Equal(t, dep.Git.Tag, "") 117 118 dep, err = ParseOpt(&opt.RegistryOptions{ 119 Git: &opt.GitOptions{ 120 Url: "test.git", 121 Branch: "", 122 Commit: "test_commit", 123 Tag: "", 124 }, 125 }) 126 assert.Equal(t, err, nil) 127 assert.Equal(t, dep.Name, "test") 128 assert.Equal(t, dep.FullName, "test_test_commit") 129 assert.Equal(t, dep.Url, "test.git") 130 assert.Equal(t, dep.Branch, "") 131 assert.Equal(t, dep.Commit, "test_commit") 132 assert.Equal(t, dep.Git.Tag, "") 133 134 dep, err = ParseOpt(&opt.RegistryOptions{ 135 Git: &opt.GitOptions{ 136 Url: "test.git", 137 Branch: "", 138 Commit: "", 139 Tag: "test_tag", 140 }, 141 }) 142 assert.Equal(t, err, nil) 143 assert.Equal(t, dep.Name, "test") 144 assert.Equal(t, dep.FullName, "test_test_tag") 145 assert.Equal(t, dep.Url, "test.git") 146 assert.Equal(t, dep.Branch, "") 147 assert.Equal(t, dep.Commit, "") 148 assert.Equal(t, dep.Git.Tag, "test_tag") 149 } 150 151 func TestLoadModFileNotExist(t *testing.T) { 152 testPath := getTestDir("mod_not_exist") 153 isExist, err := ModFileExists(testPath) 154 assert.Equal(t, isExist, false) 155 assert.Equal(t, err, nil) 156 } 157 158 func TestLoadLockFileNotExist(t *testing.T) { 159 testPath := getTestDir("mod_not_exist") 160 isExist, err := ModLockFileExists(testPath) 161 assert.Equal(t, isExist, false) 162 assert.Equal(t, err, nil) 163 } 164 165 func TestLoadModFile(t *testing.T) { 166 testPath := getTestDir("load_mod_file") 167 modFile, err := LoadModFile(testPath) 168 169 assert.Equal(t, modFile.Pkg.Name, "test_add_deps") 170 assert.Equal(t, modFile.Pkg.Version, "0.0.1") 171 assert.Equal(t, modFile.Pkg.Edition, "0.0.1") 172 173 assert.Equal(t, len(modFile.Dependencies.Deps), 3) 174 assert.Equal(t, modFile.Dependencies.Deps["name"].Name, "name") 175 assert.Equal(t, modFile.Dependencies.Deps["name"].Source.Git.Url, "test_url") 176 assert.Equal(t, modFile.Dependencies.Deps["name"].Source.Git.Tag, "test_tag") 177 assert.Equal(t, modFile.Dependencies.Deps["name"].FullName, "name_test_tag") 178 179 assert.Equal(t, modFile.Dependencies.Deps["oci_name"].Name, "oci_name") 180 assert.Equal(t, modFile.Dependencies.Deps["oci_name"].Version, "oci_tag") 181 assert.Equal(t, modFile.Dependencies.Deps["oci_name"].Source.Oci.Tag, "oci_tag") 182 assert.Equal(t, err, nil) 183 184 assert.Equal(t, modFile.Dependencies.Deps["helloworld"].Name, "helloworld") 185 assert.Equal(t, modFile.Dependencies.Deps["helloworld"].Version, "0.1.1") 186 assert.Equal(t, modFile.Dependencies.Deps["helloworld"].Source.Oci.Tag, "0.1.1") 187 assert.Equal(t, err, nil) 188 } 189 190 func TestLoadLockDeps(t *testing.T) { 191 testPath := getTestDir("load_lock_file") 192 deps, err := LoadLockDeps(testPath) 193 194 assert.Equal(t, len(deps.Deps), 2) 195 assert.Equal(t, deps.Deps["name"].Name, "name") 196 assert.Equal(t, deps.Deps["name"].Version, "test_version") 197 assert.Equal(t, deps.Deps["name"].Sum, "test_sum") 198 assert.Equal(t, deps.Deps["name"].Source.Git.Url, "test_url") 199 assert.Equal(t, deps.Deps["name"].Source.Git.Tag, "test_tag") 200 assert.Equal(t, deps.Deps["name"].FullName, "test_version") 201 202 assert.Equal(t, deps.Deps["oci_name"].Name, "oci_name") 203 assert.Equal(t, deps.Deps["oci_name"].Version, "test_version") 204 assert.Equal(t, deps.Deps["oci_name"].Sum, "test_sum") 205 assert.Equal(t, deps.Deps["oci_name"].Source.Oci.Reg, "test_reg") 206 assert.Equal(t, deps.Deps["oci_name"].Source.Oci.Repo, "test_repo") 207 assert.Equal(t, deps.Deps["oci_name"].Source.Oci.Tag, "test_oci_tag") 208 assert.Equal(t, deps.Deps["oci_name"].FullName, "test_version") 209 assert.Equal(t, err, nil) 210 } 211 212 func TestStoreModFile(t *testing.T) { 213 testPath := getTestDir("store_mod_file") 214 mfile := ModFile{ 215 HomePath: testPath, 216 Pkg: Package{ 217 Name: "test_name", 218 Edition: "0.0.1", 219 Version: "0.0.1", 220 }, 221 } 222 223 _ = mfile.StoreModFile() 224 225 expect, _ := os.ReadFile(filepath.Join(testPath, "expected.toml")) 226 got, _ := os.ReadFile(filepath.Join(testPath, "kcl.mod")) 227 assert.Equal(t, utils.RmNewline(string(got)), utils.RmNewline(string(expect))) 228 } 229 230 func TestGetFilePath(t *testing.T) { 231 testPath := getTestDir("store_mod_file") 232 mfile := ModFile{ 233 HomePath: testPath, 234 } 235 assert.Equal(t, mfile.GetModFilePath(), filepath.Join(testPath, MOD_FILE)) 236 assert.Equal(t, mfile.GetModLockFilePath(), filepath.Join(testPath, MOD_LOCK_FILE)) 237 } 238 239 func TestGenSource(t *testing.T) { 240 src, err := GenSource("git", "https://github.com/kcl-lang/kcl", "0.8.7") 241 assert.Equal(t, err, nil) 242 assert.Equal(t, src.Git.Url, "https://github.com/kcl-lang/kcl") 243 assert.Equal(t, src.Git.Tag, "0.8.7") 244 245 src, err = GenSource("oci", "oci://ghcr.io/kcl-lang/k8s", "1.24") 246 assert.Equal(t, err, nil) 247 assert.Equal(t, src.Oci.Reg, "ghcr.io") 248 assert.Equal(t, src.Oci.Repo, "kcl-lang/k8s") 249 assert.Equal(t, src.Oci.Tag, "1.24") 250 }