Debian Codegraph @

cmd/shfmt/main_test.go

raw
1 // Copyright (c) 2016, Daniel Martí <[email protected]>
2 // See LICENSE for licensing information
3
4 package main
5
6 import (
7 "bytes"
8 "flag"
9 "os"
10 "path/filepath"
11 "runtime"
12 "testing"
13
14 "github.com/rogpeppe/go-internal/testscript"
15 )
16
17 func TestMain(m *testing.M) {
18 if runtime.GOOS == "js" {
19 // Every test here runs shfmt as a subprocess, which js/wasm can't do.
20 return
21 }
22 testscript.Main(m, map[string]func(){
23 "shfmt": main,
24 })
25 }
26
27 var update = flag.Bool("u", false, "update testscript output files")
28
29 func TestScript(t *testing.T) {
30 t.Parallel()
31 testscript.Run(t, testscript.Params{
32 Dir: filepath.Join("testdata", "script"),
33 UpdateScripts: *update,
34 RequireExplicitExec: true,
35 Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
36 // txtar archive files always end with a newline,
37 // so this helps test files which do not end with one.
38 "trim-newline": func(ts *testscript.TestScript, neg bool, args []string) {
39 if neg || len(args) == 0 {
40 ts.Fatalf("usage: trim-newline file...")
41 }
42 for _, arg := range args {
43 path := ts.MkAbs(arg)
44 data, err := os.ReadFile(path)
45 ts.Check(err)
46 trimmed, ok := bytes.CutSuffix(data, []byte("\n"))
47 if !ok {
48 ts.Fatalf("%s does not end with a newline", arg)
49 }
50 ts.Check(os.WriteFile(path, trimmed, 0o666))
51 }
52 },
53 },
54 })
55 }