Allow tests to bypass PathForSource existence checks

Forcing every test to specify every file it wants to pass to
PathForSource or PathForModuleSrc is painful to maintain and
doesn't add any value.  Allow tests to reference paths through
PathForSource and PathForModuleSrc without specifying them in
the mock FS.

Test: all soong tests
Change-Id: Ia8a8fd965a338d0645b3721314bf91f50146ad21
This commit is contained in:
Colin Cross 2020-06-07 16:56:32 -07:00
parent bf488e10e8
commit 5e6a797982
2 changed files with 10 additions and 2 deletions

View file

@ -111,6 +111,10 @@ type config struct {
fs pathtools.FileSystem fs pathtools.FileSystem
mockBpList string mockBpList string
// If testAllowNonExistentPaths is true then PathForSource and PathForModuleSrc won't error
// in tests when a path doesn't exist.
testAllowNonExistentPaths bool
OncePer OncePer
} }
@ -230,6 +234,10 @@ func TestConfig(buildDir string, env map[string]string, bp string, fs map[string
buildDir: buildDir, buildDir: buildDir,
captureBuild: true, captureBuild: true,
env: envCopy, env: envCopy,
// Set testAllowNonExistentPaths so that test contexts don't need to specify every path
// passed to PathForSource or PathForModuleSrc.
testAllowNonExistentPaths: true,
} }
config.deviceConfig = &deviceConfig{ config.deviceConfig = &deviceConfig{
config: config, config: config,

View file

@ -405,7 +405,7 @@ func expandOneSrcPath(ctx ModuleContext, s string, expandedExcludes []string) (P
p := pathForModuleSrc(ctx, s) p := pathForModuleSrc(ctx, s)
if exists, _, err := ctx.Config().fs.Exists(p.String()); err != nil { if exists, _, err := ctx.Config().fs.Exists(p.String()); err != nil {
reportPathErrorf(ctx, "%s: %s", p, err.Error()) reportPathErrorf(ctx, "%s: %s", p, err.Error())
} else if !exists { } else if !exists && !ctx.Config().testAllowNonExistentPaths {
reportPathErrorf(ctx, "module source path %q does not exist", p) reportPathErrorf(ctx, "module source path %q does not exist", p)
} }
@ -798,7 +798,7 @@ func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
} }
} else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil { } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
reportPathErrorf(ctx, "%s: %s", path, err.Error()) reportPathErrorf(ctx, "%s: %s", path, err.Error())
} else if !exists { } else if !exists && !ctx.Config().testAllowNonExistentPaths {
reportPathErrorf(ctx, "source path %q does not exist", path) reportPathErrorf(ctx, "source path %q does not exist", path)
} }
return path return path