blob: a06c3e9314841d84eaa7ccd40a466024b63cc012 [file] [log] [blame]
Dan Willemsen29f88272017-02-18 18:12:41 -08001// Copyright 2017 Google Inc. All rights reserved.
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// 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
15package build
16
Dan Willemsenfe8b6452018-05-12 18:34:24 -070017import (
Patrice Arruda7cc20742020-06-10 18:48:01 +000018 "bytes"
Dan Willemsenfe8b6452018-05-12 18:34:24 -070019 "io/ioutil"
20 "os"
21 "path/filepath"
22 "testing"
23
24 "android/soong/ui/logger"
25)
26
27func TestEnsureEmptyDirs(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070028 t.Parallel()
Dan Willemsenfe8b6452018-05-12 18:34:24 -070029 ctx := testContext()
30 defer logger.Recover(func(err error) {
31 t.Error(err)
32 })
33
34 tmpDir, err := ioutil.TempDir("", "")
35 if err != nil {
36 t.Fatal(err)
37 }
38 defer func() {
39 err := os.RemoveAll(tmpDir)
40 if err != nil {
41 t.Errorf("Error removing tmpDir: %v", err)
42 }
43 }()
44
45 ensureEmptyDirectoriesExist(ctx, filepath.Join(tmpDir, "a/b"))
46
47 err = os.Chmod(filepath.Join(tmpDir, "a"), 0555)
48 if err != nil {
49 t.Fatalf("Failed to chown: %v", err)
50 }
51
52 ensureEmptyDirectoriesExist(ctx, filepath.Join(tmpDir, "a"))
53}
Patrice Arruda7cc20742020-06-10 18:48:01 +000054
55func TestCopyFile(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070056 t.Parallel()
Patrice Arruda7cc20742020-06-10 18:48:01 +000057 tmpDir, err := ioutil.TempDir("", "test_copy_file")
58 if err != nil {
59 t.Fatalf("failed to create temporary directory to hold test text files: %v", err)
60 }
61 defer os.Remove(tmpDir)
62
63 data := []byte("fake data")
64 src := filepath.Join(tmpDir, "src.txt")
65 if err := ioutil.WriteFile(src, data, 0755); err != nil {
66 t.Fatalf("failed to create a src file %q for copying: %v", src, err)
67 }
68
69 dst := filepath.Join(tmpDir, "dst.txt")
70
71 l, err := copyFile(src, dst)
72 if err != nil {
73 t.Fatalf("got %v, expecting nil error on copyFile operation", err)
74 }
75
76 if l != int64(len(data)) {
77 t.Errorf("got %d, expecting %d for copied bytes", l, len(data))
78 }
79
80 dstData, err := ioutil.ReadFile(dst)
81 if err != nil {
82 t.Fatalf("got %v, expecting nil error reading dst %q file", err, dst)
83 }
84
85 if bytes.Compare(data, dstData) != 0 {
86 t.Errorf("got %q, expecting data %q from dst %q text file", string(data), string(dstData), dst)
87 }
88}
89
90func TestCopyFileErrors(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070091 t.Parallel()
Patrice Arruda7cc20742020-06-10 18:48:01 +000092 tmpDir, err := ioutil.TempDir("", "test_copy_file_errors")
93 if err != nil {
94 t.Fatalf("failed to create temporary directory to hold test text files: %v", err)
95 }
96 defer os.Remove(tmpDir)
97
98 srcExists := filepath.Join(tmpDir, "src_exist.txt")
99 if err := ioutil.WriteFile(srcExists, []byte("fake data"), 0755); err != nil {
100 t.Fatalf("failed to create a src file %q for copying: %v", srcExists, err)
101 }
102
103 tests := []struct {
104 description string
105 src string
106 dst string
107 }{{
108 description: "src file does not exist",
109 src: "/src/not/exist",
110 dst: "/dst/not/exist",
111 }, {
112 description: "dst directory does not exist",
113 src: srcExists,
114 dst: "/dst/not/exist",
115 }}
116
117 for _, tt := range tests {
118 t.Run(tt.description, func(t *testing.T) {
119 if _, err := copyFile(tt.src, tt.dst); err == nil {
120 t.Errorf("got nil, expecting error")
121 }
122 })
123 }
124}