Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 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 | |
| 15 | package bp2build |
| 16 | |
| 17 | import ( |
| 18 | "sort" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 22 | type filepath struct { |
| 23 | dir string |
| 24 | basename string |
| 25 | } |
| 26 | |
| 27 | func assertFilecountsAreEqual(t *testing.T, actual []BazelFile, expected []filepath) { |
| 28 | if a, e := len(actual), len(expected); a != e { |
| 29 | t.Errorf("Expected %d files, got %d", e, a) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func assertFileContent(t *testing.T, actual []BazelFile, expected []filepath) { |
| 34 | for i := range actual { |
| 35 | if g, w := actual[i], expected[i]; g.Dir != w.dir || g.Basename != w.basename { |
| 36 | t.Errorf("Did not find expected file %s/%s", g.Dir, g.Basename) |
| 37 | } else if g.Basename == "BUILD" || g.Basename == "WORKSPACE" { |
| 38 | if g.Contents != "" { |
| 39 | t.Errorf("Expected %s to have no content.", g) |
| 40 | } |
| 41 | } else if g.Contents == "" { |
| 42 | t.Errorf("Contents of %s unexpected empty.", g) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func sortFiles(files []BazelFile) { |
| 48 | sort.Slice(files, func(i, j int) bool { |
| 49 | if dir1, dir2 := files[i].Dir, files[j].Dir; dir1 == dir2 { |
| 50 | return files[i].Basename < files[j].Basename |
| 51 | } else { |
| 52 | return dir1 < dir2 |
| 53 | } |
| 54 | }) |
| 55 | } |
| 56 | |
| 57 | func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) { |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame^] | 58 | files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 59 | expectedFilePaths := []filepath{ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 60 | { |
| 61 | dir: "", |
| 62 | basename: "BUILD", |
| 63 | }, |
| 64 | { |
| 65 | dir: "", |
| 66 | basename: "WORKSPACE", |
| 67 | }, |
| 68 | { |
| 69 | dir: bazelRulesSubDir, |
| 70 | basename: "BUILD", |
| 71 | }, |
| 72 | { |
| 73 | dir: bazelRulesSubDir, |
| 74 | basename: "providers.bzl", |
| 75 | }, |
| 76 | { |
| 77 | dir: bazelRulesSubDir, |
| 78 | basename: "soong_module.bzl", |
| 79 | }, |
| 80 | } |
| 81 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 82 | assertFilecountsAreEqual(t, files, expectedFilePaths) |
| 83 | sortFiles(files) |
| 84 | assertFileContent(t, files, expectedFilePaths) |
| 85 | } |
| 86 | |
| 87 | func TestCreateBazelFiles_Bp2Build_AddsTopLevelFiles(t *testing.T) { |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame^] | 88 | files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, Bp2Build) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 89 | expectedFilePaths := []filepath{ |
| 90 | { |
| 91 | dir: "", |
| 92 | basename: "BUILD", |
| 93 | }, |
| 94 | { |
| 95 | dir: "", |
| 96 | basename: "WORKSPACE", |
| 97 | }, |
| 98 | { |
| 99 | dir: bazelRulesSubDir, |
| 100 | basename: "BUILD", |
| 101 | }, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 104 | assertFilecountsAreEqual(t, files, expectedFilePaths) |
| 105 | sortFiles(files) |
| 106 | assertFileContent(t, files, expectedFilePaths) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 107 | } |