| 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 |  | 
| Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 22 | type bazelFilepath struct { | 
| Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 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) { | 
| usta | 40caf95 | 2023-08-04 16:52:14 -0400 | [diff] [blame] | 28 | files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView) | 
| Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 29 | expectedFilePaths := []bazelFilepath{ | 
| Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 30 | { | 
|  | 31 | dir:      "", | 
| Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 32 | basename: "BUILD.bazel", | 
| Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 33 | }, | 
|  | 34 | { | 
|  | 35 | dir:      "", | 
|  | 36 | basename: "WORKSPACE", | 
|  | 37 | }, | 
|  | 38 | { | 
|  | 39 | dir:      bazelRulesSubDir, | 
| Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 40 | basename: "BUILD.bazel", | 
| Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 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) | 
| Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 72 | } else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" { | 
| Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 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 | } |