1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16package gitee_common 17 18import ( 19 "fotff/vcs" 20 "os" 21 "path/filepath" 22 "strings" 23 "testing" 24) 25 26func TestMain(m *testing.M) { 27 defer os.RemoveAll(".fotff") 28 defer os.RemoveAll("logs") 29 m.Run() 30} 31 32func TestManager_Steps(t *testing.T) { 33 m := &Manager{Workspace: "./testdata", Branch: "master"} 34 defer func() { 35 entries, _ := os.ReadDir(m.Workspace) 36 for _, e := range entries { 37 if strings.HasPrefix(e.Name(), "version") { 38 continue 39 } 40 os.RemoveAll(filepath.Join(m.Workspace, e.Name())) 41 } 42 }() 43 tests := []struct { 44 name string 45 from, to string 46 stepsNum int 47 }{ 48 { 49 name: "15 MR of 15 steps in 12 repo, with 1 path change", 50 from: "version-Daily_Version-dayu200-20221201_080109-dayu200", 51 to: "version-Daily_Version-dayu200-20221201_100141-dayu200", 52 stepsNum: 15, 53 }, 54 { 55 name: "27 MR of 25 steps in 21 repo, with 1 repo add", 56 from: "version-Daily_Version-dayu200-20221213_110027-dayu200", 57 to: "version-Daily_Version-dayu200-20221213_140150-dayu200", 58 stepsNum: 25, 59 }, 60 { 61 name: "15 MR of 14 steps in 14 repo, no structure change", 62 from: "version-Daily_Version-dayu200-20221214_100124-dayu200", 63 to: "version-Daily_Version-dayu200-20221214_110125-dayu200", 64 stepsNum: 14, 65 }, 66 } 67 for _, tt := range tests { 68 t.Run(tt.name, func(t *testing.T) { 69 ret, err := m.Steps(tt.from, tt.to) 70 if err != nil { 71 t.Fatalf("err: expcect: <nil>, actual: %v", err) 72 } 73 if len(ret) != tt.stepsNum { 74 t.Fatalf("steps num: expcect: %d, actual: %v", tt.stepsNum, len(ret)) 75 } 76 if tt.stepsNum == 0 { 77 return 78 } 79 mLast, err := vcs.ParseManifestFile(filepath.Join(m.Workspace, ret[len(ret)-1], "manifest_tag.xml")) 80 if err != nil { 81 t.Fatalf("err: expcect: <nil>, actual: %v", err) 82 } 83 mLastMD5, err := mLast.Standardize() 84 if err != nil { 85 t.Fatalf("err: expcect: <nil>, actual: %v", err) 86 } 87 expected, err := vcs.ParseManifestFile(filepath.Join(m.Workspace, tt.to, "manifest_tag.xml")) 88 if err != nil { 89 t.Fatalf("err: expcect: <nil>, actual: %v", err) 90 } 91 expectedMD5, err := expected.Standardize() 92 if err != nil { 93 t.Fatalf("err: expcect: <nil>, actual: %v", err) 94 } 95 if mLastMD5 != expectedMD5 { 96 t.Errorf("steps result: expect: %s, actual: %s", expectedMD5, mLastMD5) 97 } 98 }) 99 } 100} 101