1// Copyright 2020 Google LLC
2//
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//     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	"testing"
22)
23
24func TestCopy(t *testing.T) {
25	// Setup codebase
26	codebaseDir, err := ioutil.TempDir("", "codebase")
27	if err != nil {
28		t.Error(err)
29	}
30	defer os.RemoveAll(codebaseDir)
31	project1GitDir := path.Join(codebaseDir, "project1", ".git")
32	if err = os.MkdirAll(project1GitDir, os.ModePerm); err != nil {
33		t.Error(err)
34	}
35	emptyBytes := []byte{}
36	project1File := path.Join(codebaseDir, "project1", "projectfile")
37	if err = ioutil.WriteFile(project1File, emptyBytes, os.ModePerm); err != nil {
38		t.Error(err)
39	}
40	symlink := path.Join(codebaseDir, "symlink")
41	if err = os.Symlink(path.Join(codebaseDir, "project1"), symlink); err != nil {
42		t.Error(err)
43	}
44	project2GitDir := path.Join(codebaseDir, "dirwithprojects", "project2", ".git")
45	if err = os.MkdirAll(project2GitDir, os.ModePerm); err != nil {
46		t.Error(err)
47	}
48	dirWithoutProjects := path.Join(codebaseDir, "dirwithoutprojects")
49	if err = os.Mkdir(dirWithoutProjects, os.ModePerm); err != nil {
50		t.Error(err)
51	}
52	projectSiblingFile := path.Join(codebaseDir, "dirwithprojects", "projectsiblingfile")
53	if err = ioutil.WriteFile(projectSiblingFile, emptyBytes, os.ModePerm); err != nil {
54		t.Error(err)
55	}
56	noProjectFile1 := path.Join(dirWithoutProjects, "noprojectfile1")
57	if err = ioutil.WriteFile(noProjectFile1, emptyBytes, os.ModePerm); err != nil {
58		t.Error(err)
59	}
60	noProjectFile2 := path.Join(dirWithoutProjects, "noprojectfile2")
61	if err = ioutil.WriteFile(noProjectFile2, emptyBytes, os.ModePerm); err != nil {
62		t.Error(err)
63	}
64	topFile := path.Join(codebaseDir, "topfile")
65	if err = ioutil.WriteFile(topFile, emptyBytes, os.ModePerm); err != nil {
66		t.Error(err)
67	}
68	gitProjects := []string{
69		"project1",
70		"dirwithprojects/project2",
71	}
72
73	// Set up workspace
74	workspaceDir, err := ioutil.TempDir("", "workspace")
75	if err != nil {
76		t.Error(err)
77	}
78	//This dir may already exist if the projects have been mounted
79	wsDirWithProjects := path.Join(workspaceDir, "dirwithprojects")
80	if err = os.Mkdir(wsDirWithProjects, os.ModePerm); err != nil {
81		t.Error(err)
82	}
83
84	copier := NewFileCopier()
85	if err = copier.Copy(codebaseDir, gitProjects, workspaceDir); err != nil {
86		t.Error(err)
87	}
88
89	wsTopFile := path.Join(workspaceDir, "topfile")
90	_, err = os.Stat(wsTopFile)
91	if err != nil {
92		t.Error(err)
93	}
94	wsNoProjectFile1 := path.Join(workspaceDir, "dirwithoutprojects", "noprojectfile1")
95	_, err = os.Stat(wsNoProjectFile1)
96	if err != nil {
97		t.Error(err)
98	}
99	wsNoProjectFile2 := path.Join(workspaceDir, "dirwithoutprojects", "noprojectfile2")
100	_, err = os.Stat(wsNoProjectFile2)
101	if err != nil {
102		t.Error(err)
103	}
104	wsProjectSiblingFile := path.Join(workspaceDir, "dirwithprojects", "projectsiblingfile")
105	_, err = os.Stat(wsProjectSiblingFile)
106	if err != nil {
107		t.Error(err)
108	}
109	wsSymlink := path.Join(workspaceDir, "symlink")
110	_, err = os.Stat(wsSymlink)
111	if err != nil {
112		t.Error(err)
113	}
114	//TODO: check why this is failing
115	//	if linkInfo.Mode() & os.ModeSymlink != os.ModeSymlink {
116	//		t.Error("Symlink not copied as symlink")
117	//	}
118	wsProject2Dir := path.Join(workspaceDir, "dirwithprojects", "project2")
119	_, err = os.Stat(wsProject2Dir)
120	if err == nil {
121		t.Error("Project2 mistakenly copied")
122	} else if os.IsNotExist(err) {
123		//This error is expected
124	} else {
125		t.Error(err)
126	}
127	wsProject1Dir := path.Join(workspaceDir, "project1")
128	_, err = os.Stat(wsProject1Dir)
129	if err == nil {
130		t.Error("Project1 mistakenly copied")
131	} else if os.IsNotExist(err) {
132		//This error is expected
133	} else {
134		t.Error(err)
135	}
136}
137