golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/gorebuild/pkg_darwin_test.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"os"
    10  	"os/exec"
    11  	"path/filepath"
    12  	"runtime"
    13  	"sort"
    14  	"testing"
    15  )
    16  
    17  func TestStripDarwinSig(t *testing.T) {
    18  	exe, err := os.Executable()
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	data, err := os.ReadFile(exe)
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	var log Log
    28  	stripped := StripDarwinSig(&log, "/bin/"+filepath.Base(exe), data)
    29  	for _, m := range log.Messages {
    30  		t.Log(m.Text)
    31  	}
    32  	if runtime.GOARCH != "amd64" && bytes.Equal(stripped, data) {
    33  		t.Errorf("failed to strip signature")
    34  	}
    35  }
    36  
    37  func TestIndexPkg(t *testing.T) {
    38  	dir := t.TempDir()
    39  	check := func(err error) {
    40  		if err != nil {
    41  			t.Fatal(err)
    42  		}
    43  	}
    44  
    45  	subdirs := []string{
    46  		"root/etc/paths.d",
    47  		"root/usr/local/go/subdir",
    48  		"scripts",
    49  		"resources",
    50  		"out1",
    51  		"out2",
    52  	}
    53  	for _, d := range subdirs {
    54  		check(os.MkdirAll(dir+"/"+d, 0777))
    55  	}
    56  	check(os.WriteFile(dir+"/distribution", distributionXML, 0666))
    57  	check(os.WriteFile(dir+"/root/etc/paths.d/go", []byte("ignore me!"), 0666))
    58  	check(os.WriteFile(dir+"/root/usr/local/go/hello.txt", []byte("hello world"), 0666))
    59  	check(os.WriteFile(dir+"/root/usr/local/go/subdir/fortune.txt", []byte("you will be packaged"), 0666))
    60  
    61  	cmd := exec.Command("pkgbuild",
    62  		"--identifier=org.golang.go",
    63  		"--version=1.2.3",
    64  		"--scripts=scripts",
    65  		"--root=root",
    66  		"out1/org.golang.go.pkg")
    67  	cmd.Dir = dir
    68  	out, err := cmd.CombinedOutput()
    69  	if err != nil {
    70  		t.Fatalf("pkgbuild: %v\n%s", err, out)
    71  	}
    72  
    73  	cmd = exec.Command("productbuild",
    74  		"--distribution=distribution",
    75  		"--resources=resources",
    76  		"--package-path=out1",
    77  		"out2/go.pkg")
    78  	cmd.Dir = dir
    79  	out, err = cmd.CombinedOutput()
    80  	if err != nil {
    81  		t.Fatalf("productbuild: %v\n%s", err, out)
    82  	}
    83  
    84  	data, err := os.ReadFile(dir + "/out2/go.pkg")
    85  	check(err)
    86  
    87  	var log Log
    88  	ix := indexPkg(&log, data, nil)
    89  	for _, m := range log.Messages {
    90  		t.Log(m.Text)
    91  	}
    92  	if ix == nil {
    93  		t.Fatalf("indexPkg failed")
    94  	}
    95  
    96  	var files []*CpioFile
    97  	for _, f := range ix {
    98  		files = append(files, f)
    99  	}
   100  	sort.Slice(files, func(i, j int) bool {
   101  		return files[i].Name < files[j].Name
   102  	})
   103  	if len(files) != 2 || files[0].Name != "go/hello.txt" || files[1].Name != "go/subdir/fortune.txt" {
   104  		t.Errorf("unexpected pkg contents:")
   105  		for _, f := range files {
   106  			t.Logf("%+v", *f)
   107  		}
   108  	}
   109  }
   110  
   111  var distributionXML = []byte(`<?xml version="1.0" encoding="utf-8" standalone="no"?>
   112  <installer-gui-script minSpecVersion="1">
   113    <title>Go</title>
   114    <choices-outline>
   115      <line choice="org.golang.go.choice" />
   116    </choices-outline>
   117    <choice id="org.golang.go.choice" title="Go">
   118      <pkg-ref id="org.golang.go.pkg" />
   119    </choice>
   120    <pkg-ref id="org.golang.go.pkg" auth="Root">org.golang.go.pkg</pkg-ref>
   121  </installer-gui-script>
   122  `)