mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/pack: r command create output file if not exist
Go 1.15 pack's r command creates the output file if it does not exist. The system "ar" command does this as well. Do the same. For bazelbuild/rules_go#2762. Change-Id: Icd88396b5c714b735c859a29ab29851e4301f4d2 Reviewed-on: https://go-review.googlesource.com/c/go/+/279516 Trust: Cherry Zhang <cherryyz@google.com> Run-TryBot: Cherry Zhang <cherryyz@google.com> Reviewed-by: Than McIntosh <thanm@google.com> TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
parent
4d27c4c223
commit
0aa9b4709a
2 changed files with 29 additions and 3 deletions
|
|
@ -43,7 +43,7 @@ func main() {
|
|||
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:])
|
||||
|
|
@ -124,10 +124,13 @@ func openArchive(name string, mode int, files []string) *Archive {
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -303,7 +303,7 @@ func TestIssue21703(t *testing.T) {
|
|||
}
|
||||
|
||||
// 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)
|
||||
|
||||
|
|
@ -368,6 +368,29 @@ func TestCreateWithCompilerObj(t *testing.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:]...)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue