package mac import ( "regexp" "strings" "testing" ) // xcodeWriteFlags are xcodebuild's options and build settings that name // a place it writes. var xcodeWriteFlags = regexp.MustCompile(`(-derivedDataPath|-clonedSourcePackagesDirPath|-packageCachePath|-resultBundlePath|-archivePath|-testProductsPath|-exportPath|\b(?:SYMROOT|OBJROOT|DSTROOT|SHARED_PRECOMPS_DIR|MODULE_CACHE_DIR)=)\s*("[^"]*"|'[^']*'|\S+)`) // Every xcodebuild the synthesised gate, P2 and P6 run sends Xcode's // writes into the attempt's scratch: derived data, package checkouts // and package caches each named under $TMPDIR, and nothing names the // real home. A check and P2 build the same way, so P2's verdict holds // for checks. func TestXcodeWritesStayInScratch(t *testing.T) { sims := ParseSimulators(mustRead(t, "testdata/simctl-devices.json")) gate, ok, reason := XcodeGate(woodstarTree(t, "Woodstar.xcodeproj/xcshareddata/xcschemes/Woodstar.xcscheme"), sims) if !ok || reason != "" { t.Fatalf("no gate: %q", reason) } xb := quote("/usr/bin/xcodebuild") scripts := map[string]string{ "checks gate": gate[2], "P2": P2Script(xb, quote("/usr/bin/xcrun"), "ABCDEF01-2345-6789-ABCD-EF0123456789"), "P6": p6Script(xb, "platform=iOS Simulator,id=ABCDEF01-2345-6789-ABCD-EF0123456789"), } for name, s := range scripts { calls := 0 bundles := map[string]bool{} for _, seg := range regexp.MustCompile(`&&|\|\||[;|\n()]`).Split(s, -1) { if !strings.Contains(seg, "xcodebuild") { continue } calls++ flags := []string{"-derivedDataPath", "-clonedSourcePackagesDirPath", "-packageCachePath"} if !strings.Contains(seg, " -list ") { // An action's result bundle: its own, in scratch, never // the default one in DARWIN_USER_TEMP_DIR. flags = append(flags, "-resultBundlePath") b := regexp.MustCompile(`-resultBundlePath ("[^"]*")`).FindStringSubmatch(seg) if b == nil || bundles[b[1]] { t.Errorf("%s: no result bundle of its own in %q", name, seg) } else { bundles[b[1]] = true } } for _, flag := range flags { if !strings.Contains(seg, flag+` "$TMPDIR/`) { t.Errorf("%s: %s not in the attempt's scratch in %q", name, flag, seg) } } // -derivedDataPath applies to an action on a scheme only. if strings.Contains(seg, " -target ") || !strings.Contains(seg, " -scheme ") && !strings.Contains(seg, " -list ") { t.Errorf("%s: a build without a scheme: %q", name, seg) } } if calls == 0 { t.Fatalf("%s: no xcodebuild in %q", name, s) } for _, m := range xcodeWriteFlags.FindAllStringSubmatch(s, -1) { if !strings.HasPrefix(m[2], `"$TMPDIR/`) { t.Errorf("%s: %s writes outside the attempt's scratch: %s", name, m[1], m[2]) } } for _, home := range []string{"$HOME", "${HOME", "~", "/Users/", "Library/", "CFFIXED_USER_HOME"} { if strings.Contains(s, home) { t.Errorf("%s names the real home (%s): %q", name, home, s) } } } } // The fixture's script target has the shared scheme P2 builds it with. func TestFixtureScriptScheme(t *testing.T) { raw, err := fixtures.ReadFile("fixtures/hostile/project/Hostile.xcodeproj/xcshareddata/xcschemes/HostileScript.xcscheme") if err != nil { t.Fatal(err) } pbx, _ := fixtures.ReadFile("fixtures/hostile/project/Hostile.xcodeproj/project.pbxproj") id := regexp.MustCompile(`BlueprintIdentifier = "([0-9A-F]{24})"`).FindSubmatch(raw) if id == nil || !strings.Contains(string(pbx), string(id[1])+" /* HostileScript */ = {") || !strings.Contains(string(raw), `ReferencedContainer = "container:Hostile.xcodeproj"`) { t.Fatalf("the scheme does not build HostileScript: %s", raw) } }