ar = openArchive(os.Args[2], os.O_RDONLY, os.Args[3:])
ar.scan(ar.printContents)
case 'r':
- ar = openArchive(os.Args[2], os.O_RDWR, os.Args[3:])
+ ar = openArchive(os.Args[2], os.O_RDWR|os.O_CREATE, os.Args[3:])
ar.addFiles()
case 'c':
ar = openArchive(os.Args[2], os.O_RDWR|os.O_TRUNC|os.O_CREATE, os.Args[3:])
log.Fatal(err)
}
var a *archive.Archive
- if mode&os.O_CREATE != 0 { // the c command
+ if mode&os.O_TRUNC != 0 { // the c command
a, err = archive.New(f)
} else {
a, err = archive.Parse(f, verbose)
+ if err != nil && mode&os.O_CREATE != 0 { // the r command
+ a, err = archive.New(f)
+ }
}
if err != nil {
log.Fatal(err)
}
// Test the "c" command can "see through" the archive generated by the compiler.
-// This is peculiar. (See issue )
+// This is peculiar. (See issue #43271)
func TestCreateWithCompilerObj(t *testing.T) {
testenv.MustHaveGoBuild(t)
}
}
+// 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:]...)