| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // xcodeWriteFlags are xcodebuild's options and build settings that name |
| 10 | // a place it writes. |
| 11 | var xcodeWriteFlags = regexp.MustCompile(`(-derivedDataPath|-clonedSourcePackagesDirPath|-packageCachePath|-resultBundlePath|-archivePath|-testProductsPath|-exportPath|\b(?:SYMROOT|OBJROOT|DSTROOT|SHARED_PRECOMPS_DIR|MODULE_CACHE_DIR)=)\s*("[^"]*"|'[^']*'|\S+)`) |
| 12 | |
| 13 | // Every xcodebuild the synthesised gate, P2 and P6 run sends Xcode's |
| 14 | // writes into the attempt's scratch: derived data, package checkouts |
| 15 | // and package caches each named under $TMPDIR, and nothing names the |
| 16 | // real home. A check and P2 build the same way, so P2's verdict holds |
| 17 | // for checks. |
| 18 | func TestXcodeWritesStayInScratch(t *testing.T) { |
| 19 | sims := ParseSimulators(mustRead(t, "testdata/simctl-devices.json")) |
| 20 | gate, ok, reason := XcodeGate(woodstarTree(t, "Woodstar.xcodeproj/xcshareddata/xcschemes/Woodstar.xcscheme"), sims) |
| 21 | if !ok || reason != "" { |
| 22 | t.Fatalf("no gate: %q", reason) |
| 23 | } |
| 24 | xb := quote("/usr/bin/xcodebuild") |
| 25 | scripts := map[string]string{ |
| 26 | "checks gate": gate[2], |
| 27 | "P2": P2Script(xb, quote("/usr/bin/xcrun"), "ABCDEF01-2345-6789-ABCD-EF0123456789"), |
| 28 | "P6": p6Script(xb, "platform=iOS Simulator,id=ABCDEF01-2345-6789-ABCD-EF0123456789"), |
| 29 | } |
| 30 | for name, s := range scripts { |
| 31 | calls := 0 |
| 32 | bundles := map[string]bool{} |
| 33 | for _, seg := range regexp.MustCompile(`&&|\|\||[;|\n()]`).Split(s, -1) { |
| 34 | if !strings.Contains(seg, "xcodebuild") { |
| 35 | continue |
| 36 | } |
| 37 | calls++ |
| 38 | flags := []string{"-derivedDataPath", "-clonedSourcePackagesDirPath", "-packageCachePath"} |
| 39 | if !strings.Contains(seg, " -list ") { |
| 40 | // An action's result bundle: its own, in scratch, never |
| 41 | // the default one in DARWIN_USER_TEMP_DIR. |
| 42 | flags = append(flags, "-resultBundlePath") |
| 43 | b := regexp.MustCompile(`-resultBundlePath ("[^"]*")`).FindStringSubmatch(seg) |
| 44 | if b == nil || bundles[b[1]] { |
| 45 | t.Errorf("%s: no result bundle of its own in %q", name, seg) |
| 46 | } else { |
| 47 | bundles[b[1]] = true |
| 48 | } |
| 49 | } |
| 50 | for _, flag := range flags { |
| 51 | if !strings.Contains(seg, flag+` "$TMPDIR/`) { |
| 52 | t.Errorf("%s: %s not in the attempt's scratch in %q", name, flag, seg) |
| 53 | } |
| 54 | } |
| 55 | // -derivedDataPath applies to an action on a scheme only. |
| 56 | if strings.Contains(seg, " -target ") || !strings.Contains(seg, " -scheme ") && !strings.Contains(seg, " -list ") { |
| 57 | t.Errorf("%s: a build without a scheme: %q", name, seg) |
| 58 | } |
| 59 | } |
| 60 | if calls == 0 { |
| 61 | t.Fatalf("%s: no xcodebuild in %q", name, s) |
| 62 | } |
| 63 | for _, m := range xcodeWriteFlags.FindAllStringSubmatch(s, -1) { |
| 64 | if !strings.HasPrefix(m[2], `"$TMPDIR/`) { |
| 65 | t.Errorf("%s: %s writes outside the attempt's scratch: %s", name, m[1], m[2]) |
| 66 | } |
| 67 | } |
| 68 | for _, home := range []string{"$HOME", "${HOME", "~", "/Users/", "Library/", "CFFIXED_USER_HOME"} { |
| 69 | if strings.Contains(s, home) { |
| 70 | t.Errorf("%s names the real home (%s): %q", name, home, s) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // The fixture's script target has the shared scheme P2 builds it with. |
| 77 | func TestFixtureScriptScheme(t *testing.T) { |
| 78 | raw, err := fixtures.ReadFile("fixtures/hostile/project/Hostile.xcodeproj/xcshareddata/xcschemes/HostileScript.xcscheme") |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | pbx, _ := fixtures.ReadFile("fixtures/hostile/project/Hostile.xcodeproj/project.pbxproj") |
| 83 | id := regexp.MustCompile(`BlueprintIdentifier = "([0-9A-F]{24})"`).FindSubmatch(raw) |
| 84 | if id == nil || !strings.Contains(string(pbx), string(id[1])+" /* HostileScript */ = {") || !strings.Contains(string(raw), `ReferencedContainer = "container:Hostile.xcodeproj"`) { |
| 85 | t.Fatalf("the scheme does not build HostileScript: %s", raw) |
| 86 | } |
| 87 | } |
| 88 | |