blob: 89f4ad9e10b1957d3ad6f15ef19e34f006e2e530 [file] [log] [blame]
Dan Willemsen1e775d72020-01-03 13:40:45 -08001// Copyright 2020 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
17import (
18 "android/soong/ui/logger"
19 "bytes"
20 "io/ioutil"
21 "os"
22 "path/filepath"
23 "reflect"
24 "sort"
25 "strings"
26 "testing"
27)
28
29func TestCleanOldFiles(t *testing.T) {
Dan Willemsen1e775d72020-01-03 13:40:45 -080030 dir, err := ioutil.TempDir("", "testcleanoldfiles")
31 if err != nil {
32 t.Fatal(err)
33 }
34 defer os.RemoveAll(dir)
35
36 ctx := testContext()
37 logBuf := &bytes.Buffer{}
38 ctx.Logger = logger.New(logBuf)
39
40 touch := func(names ...string) {
41 for _, name := range names {
42 if f, err := os.Create(filepath.Join(dir, name)); err != nil {
43 t.Fatal(err)
44 } else {
45 f.Close()
46 }
47 }
48 }
49 runCleanOldFiles := func(names ...string) {
50 data := []byte(strings.Join(names, " "))
51 if err := ioutil.WriteFile(filepath.Join(dir, ".installed"), data, 0666); err != nil {
52 t.Fatal(err)
53 }
54
55 cleanOldFiles(ctx, dir, ".installed")
56 }
57
58 assertFileList := func(names ...string) {
59 t.Helper()
60
61 sort.Strings(names)
62
63 var foundNames []string
64 if foundFiles, err := ioutil.ReadDir(dir); err == nil {
65 for _, fi := range foundFiles {
66 foundNames = append(foundNames, fi.Name())
67 }
68 } else {
69 t.Fatal(err)
70 }
71
72 if !reflect.DeepEqual(names, foundNames) {
73 t.Errorf("Expected a different list of files:\nwant: %v\n got: %v", names, foundNames)
74 t.Error("Log: ", logBuf.String())
75 logBuf.Reset()
76 }
77 }
78
79 // Initial list of potential files
80 runCleanOldFiles("foo", "bar")
81 touch("foo", "bar", "baz")
82 assertFileList("foo", "bar", "baz", ".installed.previous")
83
84 // This should be a no-op, as the list hasn't changed
85 runCleanOldFiles("foo", "bar")
86 assertFileList("foo", "bar", "baz", ".installed", ".installed.previous")
87
88 // This should be a no-op, as only a file was added
89 runCleanOldFiles("foo", "bar", "foo2")
90 assertFileList("foo", "bar", "baz", ".installed.previous")
91
92 // "bar" should be removed, foo2 should be ignored as it was never there
93 runCleanOldFiles("foo")
94 assertFileList("foo", "baz", ".installed.previous")
95
96 // Recreate bar, and create foo2. Ensure that they aren't removed
97 touch("bar", "foo2")
98 runCleanOldFiles("foo", "baz")
99 assertFileList("foo", "bar", "baz", "foo2", ".installed.previous")
100}