aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/pack/pack_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/pack/pack_test.go')
-rw-r--r--src/cmd/pack/pack_test.go89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/cmd/pack/pack_test.go b/src/cmd/pack/pack_test.go
index 218c7acda6..118376f9df 100644
--- a/src/cmd/pack/pack_test.go
+++ b/src/cmd/pack/pack_test.go
@@ -302,6 +302,95 @@ func TestIssue21703(t *testing.T) {
run(goBin, "tool", "compile", "-I", ".", "b.go")
}
+// Test the "c" command can "see through" the archive generated by the compiler.
+// This is peculiar. (See issue #43271)
+func TestCreateWithCompilerObj(t *testing.T) {
+ testenv.MustHaveGoBuild(t)
+
+ dir := tmpDir(t)
+ defer os.RemoveAll(dir)
+ src := filepath.Join(dir, "p.go")
+ prog := "package p; var X = 42\n"
+ err := os.WriteFile(src, []byte(prog), 0666)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ run := func(args ...string) string {
+ return doRun(t, dir, args...)
+ }
+
+ goBin := testenv.GoToolPath(t)
+ run(goBin, "build", "cmd/pack") // writes pack binary to dir
+ run(goBin, "tool", "compile", "-pack", "-o", "p.a", "p.go")
+ run("./pack", "c", "packed.a", "p.a")
+ fi, err := os.Stat(filepath.Join(dir, "p.a"))
+ if err != nil {
+ t.Fatalf("stat p.a failed: %v", err)
+ }
+ fi2, err := os.Stat(filepath.Join(dir, "packed.a"))
+ if err != nil {
+ t.Fatalf("stat packed.a failed: %v", err)
+ }
+ // For compiler-generated object file, the "c" command is
+ // expected to get (essentially) the same file back, instead
+ // of packing it into a new archive with a single entry.
+ if want, got := fi.Size(), fi2.Size(); want != got {
+ t.Errorf("packed file with different size: want %d, got %d", want, got)
+ }
+
+ // Test -linkobj flag as well.
+ run(goBin, "tool", "compile", "-linkobj", "p2.a", "-o", "p.x", "p.go")
+ run("./pack", "c", "packed2.a", "p2.a")
+ fi, err = os.Stat(filepath.Join(dir, "p2.a"))
+ if err != nil {
+ t.Fatalf("stat p2.a failed: %v", err)
+ }
+ fi2, err = os.Stat(filepath.Join(dir, "packed2.a"))
+ if err != nil {
+ t.Fatalf("stat packed2.a failed: %v", err)
+ }
+ if want, got := fi.Size(), fi2.Size(); want != got {
+ t.Errorf("packed file with different size: want %d, got %d", want, got)
+ }
+
+ run("./pack", "c", "packed3.a", "p.x")
+ fi, err = os.Stat(filepath.Join(dir, "p.x"))
+ if err != nil {
+ t.Fatalf("stat p.x failed: %v", err)
+ }
+ fi2, err = os.Stat(filepath.Join(dir, "packed3.a"))
+ if err != nil {
+ t.Fatalf("stat packed3.a failed: %v", err)
+ }
+ if want, got := fi.Size(), fi2.Size(); want != got {
+ t.Errorf("packed file with different size: want %d, got %d", want, got)
+ }
+}
+
+// Test the "r" command creates the output file if it does not exist.
+func TestRWithNonexistentFile(t *testing.T) {
+ testenv.MustHaveGoBuild(t)
+
+ dir := tmpDir(t)
+ defer os.RemoveAll(dir)
+ src := filepath.Join(dir, "p.go")
+ prog := "package p; var X = 42\n"
+ err := os.WriteFile(src, []byte(prog), 0666)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ run := func(args ...string) string {
+ return doRun(t, dir, args...)
+ }
+
+ goBin := testenv.GoToolPath(t)
+ run(goBin, "build", "cmd/pack") // writes pack binary to dir
+ run(goBin, "tool", "compile", "-o", "p.o", "p.go")
+ run("./pack", "r", "p.a", "p.o") // should succeed
+}
+
// doRun runs a program in a directory and returns the output.
func doRun(t *testing.T, dir string, args ...string) string {
cmd := exec.Command(args[0], args[1:]...)