cleanbuild_test.go 2.6 KB

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