blob: b22e9977fd6483a3defcf539c00d6179a0d6b134 [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) {
28 ctx := testContext()
29 defer logger.Recover(func(err error) {
30 t.Error(err)
31 })
32
33 tmpDir, err := ioutil.TempDir("", "")
34 if err != nil {
35 t.Fatal(err)
36 }
37 defer func() {
38 err := os.RemoveAll(tmpDir)
39 if err != nil {
40 t.Errorf("Error removing tmpDir: %v", err)
41 }
42 }()
43
44 ensureEmptyDirectoriesExist(ctx, filepath.Join(tmpDir, "a/b"))
45
46 err = os.Chmod(filepath.Join(tmpDir, "a"), 0555)
47 if err != nil {
48 t.Fatalf("Failed to chown: %v", err)
49 }
50
51 ensureEmptyDirectoriesExist(ctx, filepath.Join(tmpDir, "a"))
52}
Patrice Arruda7cc20742020-06-10 18:48:01 +000053
54func TestCopyFile(t *testing.T) {
55 tmpDir, err := ioutil.TempDir("", "test_copy_file")
56 if err != nil {
57 t.Fatalf("failed to create temporary directory to hold test text files: %v", err)
58 }
59 defer os.Remove(tmpDir)
60
61 data := []byte("fake data")
62 src := filepath.Join(tmpDir, "src.txt")
63 if err := ioutil.WriteFile(src, data, 0755); err != nil {
64 t.Fatalf("failed to create a src file %q for copying: %v", src, err)
65 }
66
67 dst := filepath.Join(tmpDir, "dst.txt")
68
69 l, err := copyFile(src, dst)
70 if err != nil {
71 t.Fatalf("got %v, expecting nil error on copyFile operation", err)
72 }
73
74 if l != int64(len(data)) {
75 t.Errorf("got %d, expecting %d for copied bytes", l, len(data))
76 }
77
78 dstData, err := ioutil.ReadFile(dst)
79 if err != nil {
80 t.Fatalf("got %v, expecting nil error reading dst %q file", err, dst)
81 }
82
83 if bytes.Compare(data, dstData) != 0 {
84 t.Errorf("got %q, expecting data %q from dst %q text file", string(data), string(dstData), dst)
85 }
86}
87
88func TestCopyFileErrors(t *testing.T) {
89 tmpDir, err := ioutil.TempDir("", "test_copy_file_errors")
90 if err != nil {
91 t.Fatalf("failed to create temporary directory to hold test text files: %v", err)
92 }
93 defer os.Remove(tmpDir)
94
95 srcExists := filepath.Join(tmpDir, "src_exist.txt")
96 if err := ioutil.WriteFile(srcExists, []byte("fake data"), 0755); err != nil {
97 t.Fatalf("failed to create a src file %q for copying: %v", srcExists, err)
98 }
99
100 tests := []struct {
101 description string
102 src string
103 dst string
104 }{{
105 description: "src file does not exist",
106 src: "/src/not/exist",
107 dst: "/dst/not/exist",
108 }, {
109 description: "dst directory does not exist",
110 src: srcExists,
111 dst: "/dst/not/exist",
112 }}
113
114 for _, tt := range tests {
115 t.Run(tt.description, func(t *testing.T) {
116 if _, err := copyFile(tt.src, tt.dst); err == nil {
117 t.Errorf("got nil, expecting error")
118 }
119 })
120 }
121}