• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Ve
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//     https://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
15package workspace
16
17import (
18	"io/ioutil"
19	"os"
20	"path"
21	"reflect"
22	"testing"
23
24	"android.googlesource.com/platform/tools/treble.git/hacksaw/bind"
25)
26
27func TestBasicCompose(t *testing.T) {
28	var err error
29	var codebaseDir string
30	if codebaseDir, err = ioutil.TempDir("", "codebase"); err != nil {
31		t.Error(err)
32	}
33	defer os.RemoveAll(codebaseDir)
34	gitDir := path.Join(codebaseDir, "project", ".git")
35	if err = os.MkdirAll(gitDir, os.ModePerm); err != nil {
36		t.Error(err)
37	}
38	repoDir := path.Join(codebaseDir, ".repo")
39	if err = os.Mkdir(repoDir, os.ModePerm); err != nil {
40		t.Error(err)
41	}
42	listContents := []byte("project")
43	listPath := path.Join(repoDir, "project.list")
44	if err = ioutil.WriteFile(listPath, listContents, os.ModePerm); err != nil {
45		t.Error(err)
46	}
47	var wsTempDir string
48	if wsTempDir, err = ioutil.TempDir("", "workspace"); err != nil {
49		t.Error(err)
50	}
51	defer os.RemoveAll(wsTempDir)
52	wsTopDir := path.Join(wsTempDir, "hacksaw")
53	if err = os.Mkdir(wsTopDir, os.ModePerm); err != nil {
54		t.Error(err)
55	}
56	fakePathBinder := bind.NewFakePathBinder()
57	wsComposer := NewComposer(fakePathBinder)
58	list, err := wsComposer.Compose(codebaseDir, wsTopDir)
59	if err != nil {
60		t.Error(err)
61	}
62	expectedList := []string{
63		path.Join(wsTopDir, ".repo"),
64		path.Join(wsTopDir, "project"),
65	}
66	if !reflect.DeepEqual(expectedList, list) {
67		t.Errorf("Expected a list of binds %v and got %v", expectedList, list)
68	}
69	list, err = wsComposer.Dismantle(wsTopDir)
70	if err != nil {
71		t.Error(err)
72	}
73	expectedList = []string{
74		path.Join(wsTopDir, "project"),
75		path.Join(wsTopDir, ".repo"),
76	}
77	if !reflect.DeepEqual(expectedList, list) {
78		t.Errorf("Expected a list of unbinds %v and got %v", expectedList, list)
79	}
80}
81
82const projectListContents = `project2/subprojectb
83project1
84project2
85project1/subprojecta`
86
87func TestBindOrder(t *testing.T) {
88	var err error
89	var codebaseDir string
90	if codebaseDir, err = ioutil.TempDir("", "codebase"); err != nil {
91		t.Error(err)
92	}
93	defer os.RemoveAll(codebaseDir)
94	project2Git := path.Join(codebaseDir, "project2", ".git")
95	if err = os.MkdirAll(project2Git, os.ModePerm); err != nil {
96		t.Error(err)
97	}
98	project1Git := path.Join(codebaseDir, "project1", ".git")
99	if err = os.MkdirAll(project1Git, os.ModePerm); err != nil {
100		t.Error(err)
101	}
102	subProjectAGit := path.Join(codebaseDir, "project1", "subprojecta", ".git")
103	if err = os.MkdirAll(subProjectAGit, os.ModePerm); err != nil {
104		t.Error(err)
105	}
106	subProjectBGit := path.Join(codebaseDir, "project2", "subprojectb", ".git")
107	if err = os.MkdirAll(subProjectBGit, os.ModePerm); err != nil {
108		t.Error(err)
109	}
110	repoDir := path.Join(codebaseDir, ".repo")
111	if err = os.Mkdir(repoDir, os.ModePerm); err != nil {
112		t.Error(err)
113	}
114	listContents := []byte(projectListContents)
115	listPath := path.Join(repoDir, "project.list")
116	if err = ioutil.WriteFile(listPath, listContents, os.ModePerm); err != nil {
117		t.Error(err)
118	}
119	var wsTempDir string
120	if wsTempDir, err = ioutil.TempDir("", "workspace"); err != nil {
121		t.Error(err)
122	}
123	defer os.RemoveAll(wsTempDir)
124	wsTopDir := path.Join(wsTempDir, "hacksaw")
125	if err = os.Mkdir(wsTopDir, os.ModePerm); err != nil {
126		t.Error(err)
127	}
128	fakePathBinder := bind.NewFakePathBinder()
129	wsComposer := NewComposer(fakePathBinder)
130	bindList, err := wsComposer.Compose(codebaseDir, wsTopDir)
131	if err != nil {
132		t.Error(err)
133	}
134	expectedList := []string{
135		path.Join(wsTopDir, ".repo"),
136		path.Join(wsTopDir, "project1"),
137		path.Join(wsTopDir, "project1", "subprojecta"),
138		path.Join(wsTopDir, "project2"),
139		path.Join(wsTopDir, "project2", "subprojectb"),
140	}
141	if !reflect.DeepEqual(expectedList, bindList) {
142		t.Errorf("Expected a list of binds %v and got %v", expectedList, bindList)
143	}
144	unbindList, err := wsComposer.Dismantle(wsTopDir)
145	if err != nil {
146		t.Error(err)
147	}
148	expectedList = []string{
149		path.Join(wsTopDir, "project2", "subprojectb"),
150		path.Join(wsTopDir, "project2"),
151		path.Join(wsTopDir, "project1", "subprojecta"),
152		path.Join(wsTopDir, "project1"),
153		path.Join(wsTopDir, ".repo"),
154	}
155	if !reflect.DeepEqual(expectedList, unbindList) {
156		t.Errorf("Expected a list of unbinds %v and got %v", expectedList, unbindList)
157	}
158}
159