-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
101 lines (93 loc) · 2.3 KB
/
Copy pathmain_test.go
File metadata and controls
101 lines (93 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"github.com/bep/helpers/envhelpers"
"github.com/rogpeppe/go-internal/testscript"
)
func TestScripts(t *testing.T) {
params := commonTestScriptsParam
params.Dir = "testscripts"
// params.TestWork = true
// params.UpdateScripts = true
testscript.Run(t, params)
}
func TestMain(m *testing.M) {
testscript.Main(m, map[string]func(){
"npmtohugomod": main,
})
}
var commonTestScriptsParam = testscript.Params{
Setup: func(env *testscript.Env) error {
setup := testSetupFunc()
return setup(env)
},
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
// tree lists a directory recursively to stdout as a simple tree.
"tree": func(ts *testscript.TestScript, neg bool, args []string) {
dirname := ts.MkAbs(args[0])
err := filepath.WalkDir(dirname, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
if d.Name() == ".git" {
return filepath.SkipDir
}
entries, err := os.ReadDir(path)
if err != nil {
return err
}
nodeType := "unknown"
for _, entry := range entries {
if !entry.IsDir() && entry.Name() == "gitjoin.txt" {
nodeType = "gitjoin"
break
}
if entry.IsDir() && entry.Name() == ".git" {
nodeType = "git"
break
}
}
rel, err := filepath.Rel(dirname, path)
if err != nil {
return err
}
if rel == "." {
fmt.Fprintf(ts.Stdout(), ". (%s)\n", nodeType)
return nil
}
depth := strings.Count(rel, string(os.PathSeparator))
prefix := strings.Repeat(" ", depth) + "└─"
if d.IsDir() {
fmt.Fprintf(ts.Stdout(), "%s%s:%s/\n", prefix, nodeType, d.Name())
} else {
fmt.Fprintf(ts.Stdout(), "%s%s:%s\n", prefix, nodeType, d.Name())
}
if nodeType == "git" {
return filepath.SkipDir
}
return nil
})
if err != nil {
ts.Fatalf("%v", err)
}
},
},
}
func testSetupFunc() func(env *testscript.Env) error {
sourceDir, _ := os.Getwd()
return func(env *testscript.Env) error {
var keyVals []string
// Add some environment variables to the test script.
keyVals = append(keyVals, "SOURCE", sourceDir)
envhelpers.SetEnvVars(&env.Vars, keyVals...)
return nil
}
}