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 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 27 | func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) { |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 28 | files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 29 | expectedFilePaths := []filepath{ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 30 | { |
| 31 | dir: "", |
| 32 | basename: "BUILD", |
| 33 | }, |
| 34 | { |
| 35 | dir: "", |
| 36 | basename: "WORKSPACE", |
| 37 | }, |
| 38 | { |
| 39 | dir: bazelRulesSubDir, |
| 40 | basename: "BUILD", |
| 41 | }, |
| 42 | { |
| 43 | dir: bazelRulesSubDir, |
| 44 | basename: "providers.bzl", |
| 45 | }, |
| 46 | { |
| 47 | dir: bazelRulesSubDir, |
| 48 | basename: "soong_module.bzl", |
| 49 | }, |
| 50 | } |
| 51 | |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 52 | // Compare number of files |
| 53 | if a, e := len(files), len(expectedFilePaths); a != e { |
| 54 | t.Errorf("Expected %d files, got %d", e, a) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 57 | // Sort the files to be deterministic |
| 58 | sort.Slice(files, func(i, j int) bool { |
| 59 | if dir1, dir2 := files[i].Dir, files[j].Dir; dir1 == dir2 { |
| 60 | return files[i].Basename < files[j].Basename |
| 61 | } else { |
| 62 | return dir1 < dir2 |
| 63 | } |
| 64 | }) |
| 65 | |
| 66 | // Compare the file contents |
| 67 | for i := range files { |
| 68 | actualFile, expectedFile := files[i], expectedFilePaths[i] |
| 69 | |
| 70 | if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename { |
| 71 | t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename) |
| 72 | } else if actualFile.Basename == "BUILD" || actualFile.Basename == "WORKSPACE" { |
| 73 | if actualFile.Contents != "" { |
| 74 | t.Errorf("Expected %s to have no content.", actualFile) |
| 75 | } |
| 76 | } else if actualFile.Contents == "" { |
| 77 | t.Errorf("Contents of %s unexpected empty.", actualFile) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestCreateBazelFiles_Bp2Build_CreatesNoFilesWithNoTargets(t *testing.T) { |
| 83 | files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, Bp2Build) |
| 84 | if len(files) != 0 { |
| 85 | t.Errorf("Expected no files, got %d", len(files)) |
| 86 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 87 | } |