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