allow_list_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 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 main
  15. import (
  16. "archive/zip"
  17. "bytes"
  18. "reflect"
  19. "testing"
  20. )
  21. func bytesToZipArtifactFile(name string, data []byte) *ZipArtifactFile {
  22. buf := &bytes.Buffer{}
  23. w := zip.NewWriter(buf)
  24. f, err := w.Create(name)
  25. if err != nil {
  26. panic(err)
  27. }
  28. _, err = f.Write(data)
  29. if err != nil {
  30. panic(err)
  31. }
  32. w.Close()
  33. r, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
  34. if err != nil {
  35. panic(err)
  36. }
  37. return &ZipArtifactFile{r.File[0]}
  38. }
  39. var f1a = bytesToZipArtifactFile("dir/f1", []byte(`
  40. a
  41. foo: bar
  42. c
  43. `))
  44. var f1b = bytesToZipArtifactFile("dir/f1", []byte(`
  45. a
  46. foo: baz
  47. c
  48. `))
  49. var f2 = bytesToZipArtifactFile("dir/f2", nil)
  50. func Test_applyAllowLists(t *testing.T) {
  51. type args struct {
  52. diff zipDiff
  53. allowLists []allowList
  54. }
  55. tests := []struct {
  56. name string
  57. args args
  58. want zipDiff
  59. wantErr bool
  60. }{
  61. {
  62. name: "simple",
  63. args: args{
  64. diff: zipDiff{
  65. onlyInA: []*ZipArtifactFile{f1a, f2},
  66. },
  67. allowLists: []allowList{{path: "dir/f1"}},
  68. },
  69. want: zipDiff{
  70. onlyInA: []*ZipArtifactFile{f2},
  71. },
  72. },
  73. {
  74. name: "glob",
  75. args: args{
  76. diff: zipDiff{
  77. onlyInA: []*ZipArtifactFile{f1a, f2},
  78. },
  79. allowLists: []allowList{{path: "dir/*"}},
  80. },
  81. want: zipDiff{},
  82. },
  83. {
  84. name: "modified",
  85. args: args{
  86. diff: zipDiff{
  87. modified: [][2]*ZipArtifactFile{{f1a, f1b}},
  88. },
  89. allowLists: []allowList{{path: "dir/*"}},
  90. },
  91. want: zipDiff{},
  92. },
  93. {
  94. name: "matching lines",
  95. args: args{
  96. diff: zipDiff{
  97. modified: [][2]*ZipArtifactFile{{f1a, f1b}},
  98. },
  99. allowLists: []allowList{{path: "dir/*", ignoreMatchingLines: []string{"foo: .*"}}},
  100. },
  101. want: zipDiff{},
  102. },
  103. }
  104. for _, tt := range tests {
  105. t.Run(tt.name, func(t *testing.T) {
  106. got, err := applyAllowLists(tt.args.diff, tt.args.allowLists)
  107. if (err != nil) != tt.wantErr {
  108. t.Errorf("Test_applyAllowLists() error = %v, wantErr %v", err, tt.wantErr)
  109. return
  110. }
  111. if !reflect.DeepEqual(got, tt.want) {
  112. t.Errorf("Test_applyAllowLists() = %v, want %v", got, tt.want)
  113. }
  114. })
  115. }
  116. }