| 1 |
// Copyright 2022 Google LLC |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style license that can be |
| 4 |
// found in the LICENSE file. |
| 5 |
|
| 6 |
package exporter |
| 7 |
|
| 8 |
import ( |
| 9 |
"bytes" |
| 10 |
"path/filepath" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/mock" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
"go.skia.org/skia/bazel/exporter/build_proto/build" |
| 17 |
"go.skia.org/skia/bazel/exporter/interfaces/mocks" |
| 18 |
"google.golang.org/protobuf/proto" |
| 19 |
) |
| 20 |
|
| 21 |
// The expected gn/core.gni file contents for createCoreSourcesQueryResult(). |
| 22 |
// This expected result is handmade. |
| 23 |
const publicSrcsExpectedGNI = `# DO NOT EDIT: This is a generated file. |
| 24 |
# See //bazel/exporter_tool/README.md for more information. |
| 25 |
# |
| 26 |
# The sources of truth are: |
| 27 |
# //src/core/BUILD.bazel |
| 28 |
# //src/opts/BUILD.bazel |
| 29 |
|
| 30 |
# To update this file, run make -C bazel generate_gni |
| 31 |
|
| 32 |
_src = get_path_info("../src", "abspath") |
| 33 |
|
| 34 |
# List generated by Bazel rules: |
| 35 |
# //src/core:core_srcs |
| 36 |
# //src/opts:private_hdrs |
| 37 |
skia_core_sources = [ |
| 38 |
"$_src/core/SkAAClip.cpp", |
| 39 |
"$_src/core/SkATrace.cpp", |
| 40 |
"$_src/core/SkAlphaRuns.cpp", |
| 41 |
"$_src/opts/SkBitmapProcState_opts.h", |
| 42 |
"$_src/opts/SkBlitMask_opts.h", |
| 43 |
"$_src/opts/SkBlitRow_opts.h", |
| 44 |
] |
| 45 |
|
| 46 |
` |
| 47 |
|
| 48 |
var exportDescs = []GNIExportDesc{ |
| 49 |
{GNI: "gn/core.gni", Vars: []GNIFileListExportDesc{ |
| 50 |
{Var: "skia_core_sources", |
| 51 |
Rules: []string{ |
| 52 |
"//src/core:core_srcs", |
| 53 |
"//src/opts:private_hdrs", |
| 54 |
}}}, |
| 55 |
}, |
| 56 |
} |
| 57 |
|
| 58 |
var testExporterParams = GNIExporterParams{ |
| 59 |
WorkspaceDir: "/path/to/workspace", |
| 60 |
ExportDescs: exportDescs, |
| 61 |
} |
| 62 |
|
| 63 |
func createCoreSourcesQueryResult() *build.QueryResult { |
| 64 |
qr := build.QueryResult{} |
| 65 |
ruleDesc := build.Target_RULE |
| 66 |
|
| 67 |
// Rule #1 |
| 68 |
srcs := []string{ |
| 69 |
"//src/core:SkAAClip.cpp", |
| 70 |
"//src/core:SkATrace.cpp", |
| 71 |
"//src/core:SkAlphaRuns.cpp", |
| 72 |
} |
| 73 |
r1 := createTestBuildRule("//src/core:core_srcs", "filegroup", |
| 74 |
"/path/to/workspace/src/core/BUILD.bazel:376:20", srcs) |
| 75 |
t1 := build.Target{Rule: r1, Type: &ruleDesc} |
| 76 |
qr.Target = append(qr.Target, &t1) |
| 77 |
|
| 78 |
// Rule #2 |
| 79 |
srcs = []string{ |
| 80 |
"//src/opts:SkBitmapProcState_opts.h", |
| 81 |
"//src/opts:SkBlitMask_opts.h", |
| 82 |
"//src/opts:SkBlitRow_opts.h", |
| 83 |
} |
| 84 |
r2 := createTestBuildRule("//src/opts:private_hdrs", "filegroup", |
| 85 |
"/path/to/workspace/src/opts/BUILD.bazel:26:10", srcs) |
| 86 |
t2 := build.Target{Rule: r2, Type: &ruleDesc} |
| 87 |
qr.Target = append(qr.Target, &t2) |
| 88 |
return &qr |
| 89 |
} |
| 90 |
|
| 91 |
func TestGNIExporterExport_ValidInput_Success(t *testing.T) { |
| 92 |
qr := createCoreSourcesQueryResult() |
| 93 |
protoData, err := proto.Marshal(qr) |
| 94 |
require.NoError(t, err) |
| 95 |
|
| 96 |
fs := mocks.NewFileSystem(t) |
| 97 |
var contents bytes.Buffer |
| 98 |
fs.On("OpenFile", mock.Anything).Once().Run(func(args mock.Arguments) { |
| 99 |
path := args.String(0) |
| 100 |
assert.True(t, filepath.IsAbs(path)) |
| 101 |
assert.Equal(t, "/path/to/workspace/gn/core.gni", filepath.ToSlash(path)) |
| 102 |
}).Return(&contents, nil).Once() |
| 103 |
e := NewGNIExporter(testExporterParams, fs) |
| 104 |
qcmd := mocks.NewQueryCommand(t) |
| 105 |
qcmd.On("Read", mock.Anything).Return(protoData, nil).Once() |
| 106 |
err = e.Export(qcmd) |
| 107 |
require.NoError(t, err) |
| 108 |
|
| 109 |
assert.Equal(t, publicSrcsExpectedGNI, contents.String()) |
| 110 |
} |
| 111 |
|
| 112 |
func TestMakeRelativeFilePathForGNI_MatchingRootDir_Success(t *testing.T) { |
| 113 |
test := func(name, target, expectedPath string) { |
| 114 |
t.Run(name, func(t *testing.T) { |
| 115 |
path, err := makeRelativeFilePathForGNI(target) |
| 116 |
require.NoError(t, err) |
| 117 |
assert.Equal(t, expectedPath, path) |
| 118 |
}) |
| 119 |
} |
| 120 |
|
| 121 |
test("src", "src/core/file.cpp", "$_src/core/file.cpp") |
| 122 |
test("include", "include/core/file.h", "$_include/core/file.h") |
| 123 |
test("modules", "modules/mod/file.cpp", "$_modules/mod/file.cpp") |
| 124 |
} |
| 125 |
|
| 126 |
func TestMakeRelativeFilePathForGNI_IndalidInput_ReturnError(t *testing.T) { |
| 127 |
test := func(name, target string) { |
| 128 |
t.Run(name, func(t *testing.T) { |
| 129 |
_, err := makeRelativeFilePathForGNI(target) |
| 130 |
assert.Error(t, err) |
| 131 |
}) |
| 132 |
} |
| 133 |
|
| 134 |
test("EmptyString", "") |
| 135 |
test("UnsupportedRootDir", "//valid/rule/incorrect/root/dir:file.cpp") |
| 136 |
} |
| 137 |
|
| 138 |
func TestIsHeaderFile_HeaderFiles_ReturnTrue(t *testing.T) { |
| 139 |
test := func(name, path string) { |
| 140 |
t.Run(name, func(t *testing.T) { |
| 141 |
assert.True(t, isHeaderFile(path)) |
| 142 |
}) |
| 143 |
} |
| 144 |
|
| 145 |
test("LowerH", "path/to/file.h") |
| 146 |
test("UpperH", "path/to/file.H") |
| 147 |
test("MixedHpp", "path/to/file.Hpp") |
| 148 |
} |
| 149 |
|
| 150 |
func TestIsHeaderFile_NonHeaderFiles_ReturnTrue(t *testing.T) { |
| 151 |
test := func(name, path string) { |
| 152 |
t.Run(name, func(t *testing.T) { |
| 153 |
assert.False(t, isHeaderFile(path)) |
| 154 |
}) |
| 155 |
} |
| 156 |
|
| 157 |
test("EmptyString", "") |
| 158 |
test("DirPath", "/path/to/dir") |
| 159 |
test("C++Source", "/path/to/file.cpp") |
| 160 |
test("DotHInDir", "/path/to/dir.h/file.cpp") |
| 161 |
test("Go", "main.go") |
| 162 |
} |
| 163 |
|
| 164 |
func TestFileListContainsOnlyCppHeaderFiles_AllHeaders_ReturnsTrue(t *testing.T) { |
| 165 |
test := func(name string, paths []string) { |
| 166 |
t.Run(name, func(t *testing.T) { |
| 167 |
assert.True(t, fileListContainsOnlyCppHeaderFiles(paths)) |
| 168 |
}) |
| 169 |
} |
| 170 |
|
| 171 |
test("OneFile", []string{"file.h"}) |
| 172 |
test("Multiple", []string{"file.h", "foo.hpp"}) |
| 173 |
} |
| 174 |
|
| 175 |
func TestFileListContainsOnlyCppHeaderFiles_NotAllHeaders_ReturnsFalse(t *testing.T) { |
| 176 |
test := func(name string, paths []string) { |
| 177 |
t.Run(name, func(t *testing.T) { |
| 178 |
assert.False(t, fileListContainsOnlyCppHeaderFiles(paths)) |
| 179 |
}) |
| 180 |
} |
| 181 |
|
| 182 |
test("Nil", nil) |
| 183 |
test("HeaderFiles", []string{"file.h", "file2.cpp"}) |
| 184 |
test("GoFile", []string{"file.go"}) |
| 185 |
} |
| 186 |
|
| 187 |
func TestWorkspaceToAbsPath_ReturnsAbsolutePath(t *testing.T) { |
| 188 |
fs := mocks.NewFileSystem(t) |
| 189 |
e := NewGNIExporter(testExporterParams, fs) |
| 190 |
require.NotNil(t, e) |
| 191 |
|
| 192 |
test := func(name, input, expected string) { |
| 193 |
t.Run(name, func(t *testing.T) { |
| 194 |
assert.Equal(t, expected, e.workspaceToAbsPath(input)) |
| 195 |
}) |
| 196 |
} |
| 197 |
|
| 198 |
test("FileInDir", "foo/bar.txt", "/path/to/workspace/foo/bar.txt") |
| 199 |
test("DirInDir", "foo/bar", "/path/to/workspace/foo/bar") |
| 200 |
test("RootFile", "root.txt", "/path/to/workspace/root.txt") |
| 201 |
test("WorkspaceDir", "", "/path/to/workspace") |
| 202 |
} |
| 203 |
|
| 204 |
func TestAbsToWorkspacePath_PathInWorkspace_ReturnsRelativePath(t *testing.T) { |
| 205 |
fs := mocks.NewFileSystem(t) |
| 206 |
e := NewGNIExporter(testExporterParams, fs) |
| 207 |
require.NotNil(t, e) |
| 208 |
|
| 209 |
test := func(name, input, expected string) { |
| 210 |
t.Run(name, func(t *testing.T) { |
| 211 |
path, err := e.absToWorkspacePath(input) |
| 212 |
assert.NoError(t, err) |
| 213 |
assert.Equal(t, expected, path) |
| 214 |
}) |
| 215 |
} |
| 216 |
|
| 217 |
test("FileInDir", "/path/to/workspace/foo/bar.txt", "foo/bar.txt") |
| 218 |
test("DirInDir", "/path/to/workspace/foo/bar", "foo/bar") |
| 219 |
test("RootFile", "/path/to/workspace/root.txt", "root.txt") |
| 220 |
test("RootFile", "/path/to/workspace/世界", "世界") |
| 221 |
test("WorkspaceDir", "/path/to/workspace", "") |
| 222 |
} |
| 223 |
|
| 224 |
func TestAbsToWorkspacePath_PathNotInWorkspace_ReturnsError(t *testing.T) { |
| 225 |
fs := mocks.NewFileSystem(t) |
| 226 |
e := NewGNIExporter(testExporterParams, fs) |
| 227 |
require.NotNil(t, e) |
| 228 |
|
| 229 |
_, err := e.absToWorkspacePath("/path/to/file.txt") |
| 230 |
assert.Error(t, err) |
| 231 |
} |
| 232 |
|
| 233 |
func TestGetGNILineVariable_LinesWithVariables_ReturnVariable(t *testing.T) { |
| 234 |
test := func(name, inputLine, expected string) { |
| 235 |
t.Run(name, func(t *testing.T) { |
| 236 |
assert.Equal(t, expected, getGNILineVariable(inputLine)) |
| 237 |
}) |
| 238 |
} |
| 239 |
|
| 240 |
test("EqualWithSpaces", `foo = [ "something" ]`, "foo") |
| 241 |
test("EqualNoSpaces", `foo=[ "something" ]`, "foo") |
| 242 |
test("EqualSpaceBefore", `foo =[ "something" ]`, "foo") |
| 243 |
test("MultilineList", `foo = [`, "foo") |
| 244 |
} |
| 245 |
|
| 246 |
func TestGetGNILineVariable_LinesWithVariables_NoMatch(t *testing.T) { |
| 247 |
test := func(name, inputLine, expected string) { |
| 248 |
t.Run(name, func(t *testing.T) { |
| 249 |
assert.Equal(t, expected, getGNILineVariable(inputLine)) |
| 250 |
}) |
| 251 |
} |
| 252 |
|
| 253 |
test("FirstCharSpace", ` foo = [ "something" ]`, "") // Impl. requires formatted file. |
| 254 |
test("NotList", `foo = bar`, "") |
| 255 |
test("ListLiteral", `[ "something" ]`, "") |
| 256 |
test("ListInComment", `# foo = [ "something" ]`, "") |
| 257 |
test("MissingVariable", `=[ "something" ]`, "") |
| 258 |
test("EmptyString", ``, "") |
| 259 |
} |
| 260 |
|
| 261 |
func TestExtractTopLevelFolder_PathsWithTopDir_ReturnsTopDir(t *testing.T) { |
| 262 |
test := func(name, input, expected string) { |
| 263 |
t.Run(name, func(t *testing.T) { |
| 264 |
assert.Equal(t, expected, extractTopLevelFolder(input)) |
| 265 |
}) |
| 266 |
} |
| 267 |
test("TopIsDir", "foo/bar/baz.txt", "foo") |
| 268 |
test("TopIsVariable", "$_src/bar/baz.txt", "$_src") |
| 269 |
test("TopIsFile", "baz.txt", "baz.txt") |
| 270 |
test("TopIsAbsDir", "/foo/bar/baz.txt", "") |
| 271 |
} |
| 272 |
|
| 273 |
func TestExtractTopLevelFolder_PathsWithNoTopDir_ReturnsEmptyString(t *testing.T) { |
| 274 |
test := func(name, input, expected string) { |
| 275 |
t.Run(name, func(t *testing.T) { |
| 276 |
assert.Equal(t, expected, extractTopLevelFolder(input)) |
| 277 |
}) |
| 278 |
} |
| 279 |
test("EmptyString", "", "") |
| 280 |
test("EmptyAbsRoot", "/", "") |
| 281 |
test("MultipleSlashes", "///", "") |
| 282 |
} |
| 283 |
|
| 284 |
func TestAddGNIVariablesToWorkspacePaths_ValidInput_ReturnsVariables(t *testing.T) { |
| 285 |
test := func(name string, inputPaths, expected []string) { |
| 286 |
t.Run(name, func(t *testing.T) { |
| 287 |
gniPaths, err := addGNIVariablesToWorkspacePaths(inputPaths) |
| 288 |
require.NoError(t, err) |
| 289 |
assert.Equal(t, expected, gniPaths) |
| 290 |
}) |
| 291 |
} |
| 292 |
test("EmptySlice", nil, []string{}) |
| 293 |
test("AllVariables", |
| 294 |
[]string{"src/include/foo.h", |
| 295 |
"include/foo.h", |
| 296 |
"modules/foo.cpp"}, |
| 297 |
[]string{"$_src/include/foo.h", |
| 298 |
"$_include/foo.h", |
| 299 |
"$_modules/foo.cpp"}) |
| 300 |
} |
| 301 |
|
| 302 |
func TestAddGNIVariablesToWorkspacePaths_InvalidInput_ReturnsError(t *testing.T) { |
| 303 |
test := func(name string, inputPaths []string) { |
| 304 |
t.Run(name, func(t *testing.T) { |
| 305 |
_, err := addGNIVariablesToWorkspacePaths(inputPaths) |
| 306 |
assert.Error(t, err) |
| 307 |
}) |
| 308 |
} |
| 309 |
test("InvalidTopDir", []string{"nomatch/include/foo.h"}) |
| 310 |
test("RuleNotPath", []string{"//src/core:source.cpp"}) |
| 311 |
} |
| 312 |
|
| 313 |
func TestConvertTargetsToFilePaths_ValidInput_ReturnsPaths(t *testing.T) { |
| 314 |
test := func(name string, inputTargets, expected []string) { |
| 315 |
t.Run(name, func(t *testing.T) { |
| 316 |
paths, err := convertTargetsToFilePaths(inputTargets) |
| 317 |
require.NoError(t, err) |
| 318 |
assert.Equal(t, expected, paths) |
| 319 |
}) |
| 320 |
} |
| 321 |
test("EmptySlice", nil, []string{}) |
| 322 |
test("Files", |
| 323 |
[]string{"//src/include:foo.h", |
| 324 |
"//include:foo.h", |
| 325 |
"//modules:foo.cpp"}, |
| 326 |
[]string{"src/include/foo.h", |
| 327 |
"include/foo.h", |
| 328 |
"modules/foo.cpp"}) |
| 329 |
} |
| 330 |
|
| 331 |
func TestConvertTargetsToFilePaths_InvalidInput_ReturnsError(t *testing.T) { |
| 332 |
test := func(name string, inputTargets []string) { |
| 333 |
t.Run(name, func(t *testing.T) { |
| 334 |
_, err := convertTargetsToFilePaths(inputTargets) |
| 335 |
assert.Error(t, err) |
| 336 |
}) |
| 337 |
} |
| 338 |
test("EmptyString", []string{""}) |
| 339 |
test("ValidTargetEmptyString", []string{"//src/include:foo.h", ""}) |
| 340 |
test("EmptyStringValidTarget", []string{"//src/include:foo.h", ""}) |
| 341 |
} |
| 342 |
|
| 343 |
func TestRemoveDuplicate_ContainsDuplicates_SortedAndDuplicatesRemoved(t *testing.T) { |
| 344 |
files := []string{ |
| 345 |
"alpha", |
| 346 |
"beta", |
| 347 |
"gamma", |
| 348 |
"delta", |
| 349 |
"beta", |
| 350 |
"Alpha", |
| 351 |
"alpha", |
| 352 |
"path/to/file", |
| 353 |
"path/to/file2", |
| 354 |
"path/to/file", |
| 355 |
} |
| 356 |
output := removeDuplicates(files) |
| 357 |
assert.Equal(t, []string{ |
| 358 |
"Alpha", |
| 359 |
"alpha", |
| 360 |
"beta", |
| 361 |
"delta", |
| 362 |
"gamma", |
| 363 |
"path/to/file", |
| 364 |
"path/to/file2", |
| 365 |
}, output) |
| 366 |
} |
| 367 |
|
| 368 |
func TestRemoveDuplicates_NoDuplicates_ReturnsOnlySorted(t *testing.T) { |
| 369 |
files := []string{ |
| 370 |
"Beta", |
| 371 |
"ALPHA", |
| 372 |
"gamma", |
| 373 |
"path/to/file2", |
| 374 |
"path/to/file", |
| 375 |
} |
| 376 |
output := removeDuplicates(files) |
| 377 |
assert.Equal(t, []string{ |
| 378 |
"ALPHA", |
| 379 |
"Beta", |
| 380 |
"gamma", |
| 381 |
"path/to/file", |
| 382 |
"path/to/file2", |
| 383 |
}, output) |
| 384 |
} |
| 385 |
|
| 386 |
func TestGetPathToTopDir_ValidRelativePaths_ReturnsExpected(t *testing.T) { |
| 387 |
test := func(name, expected, input string) { |
| 388 |
t.Run(name, func(t *testing.T) { |
| 389 |
assert.Equal(t, expected, getPathToTopDir(input)) |
| 390 |
}) |
| 391 |
} |
| 392 |
test("TopDir", ".", "core.gni") |
| 393 |
test("OneDown", "..", "gn/core.gni") |
| 394 |
test("TwoDown", "../..", "modules/skcms/skcms.gni") |
| 395 |
} |
| 396 |
|
| 397 |
func TestGetPathToTopDir_AbsolutePath_ReturnsEmptyString(t *testing.T) { |
| 398 |
// Exporter shouldn't use absolute paths, but just to be safe. |
| 399 |
assert.Equal(t, "", getPathToTopDir("/")) |
| 400 |
} |