Skip to content

Commit

Permalink
cmd/shfmt: switch away from func() int for testscript
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdan committed Dec 26, 2024
1 parent aa946ad commit 86363cf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
22 changes: 9 additions & 13 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ func init() {
}

func main() {
os.Exit(main1())
}

func main1() int {
flag.Usage = func() {
fmt.Fprint(os.Stderr, `usage: shfmt [flags] [path ...]
Expand Down Expand Up @@ -202,19 +198,19 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
version = mod.Version
}
fmt.Println(version)
return 0
return
}
if posix.val && lang.val != syntax.LangAuto {
fmt.Fprintf(os.Stderr, "-p and -ln=lang cannot coexist\n")
return 1
os.Exit(1)
}
if list.val != "true" && list.val != "false" && list.val != "0" {
fmt.Fprintf(os.Stderr, "only -l and -l=0 allowed\n")
return 1
os.Exit(1)
}
if find.val != "true" && find.val != "false" && find.val != "0" {
fmt.Fprintf(os.Stderr, "only -f and -f=0 allowed\n")
return 1
os.Exit(1)
}
if minify.val {
simplify.val = true
Expand Down Expand Up @@ -271,17 +267,17 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
if err != errChangedWithDiff {
fmt.Fprintln(os.Stderr, err)
}
return 1
os.Exit(1)
}
return 0
return
}
if filename.val != "" {
fmt.Fprintln(os.Stderr, "-filename can only be used with stdin")
return 1
os.Exit(1)
}
if toJSON.val {
fmt.Fprintln(os.Stderr, "--to-json can only be used with stdin")
return 1
os.Exit(1)
}
status := 0
for _, path := range flag.Args() {
Expand Down Expand Up @@ -319,7 +315,7 @@ For more information and to report bugs, see https://github.com/mvdan/sh.
status = 1
}
}
return status
os.Exit(status)
}

var errChangedWithDiff = fmt.Errorf("")
Expand Down
7 changes: 3 additions & 4 deletions cmd/shfmt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ package main

import (
"flag"
"os"
"path/filepath"
"testing"

"github.com/rogpeppe/go-internal/testscript"
)

func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"shfmt": main1,
}))
testscript.Main(m, map[string]func(){
"shfmt": main,
})
}

var update = flag.Bool("u", false, "update testscript output files")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/go-quicktest/qt v1.101.0
github.com/google/go-cmp v0.6.0
github.com/google/renameio/v2 v2.0.0
github.com/rogpeppe/go-internal v1.13.1
github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a
golang.org/x/sync v0.9.0
golang.org/x/sys v0.27.0
golang.org/x/term v0.26.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a h1:w3tdWGKbLGBPtR/8/oO74W6hmz0qE5q0z9aqSAewaaM=
github.com/rogpeppe/go-internal v1.13.2-0.20241226121412-a5dc8ff20d0a/go.mod h1:S8kfXMp+yh77OxPD4fdM6YUknrZpQxLhvxzS4gDHENY=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
Expand Down

0 comments on commit 86363cf

Please sign in to comment.