1 # Test using -json flag to specify specific fields.
2
3 # Test -json produces "full" output by looking for multiple fields present.
4 go list -json .
5 stdout '"Name": "a"'
6 stdout '"Stale": true'
7 # Same thing for -json=true
8 go list -json=true .
9 stdout '"Name": "a"'
10 stdout '"Stale": true'
11
12 # Test -json=false produces non-json output.
13 go list -json=false
14 cmp stdout want-non-json.txt
15
16 # Test -json=<field> keeps only that field.
17 go list -json=Name
18 cmp stdout want-json-name.txt
19
20 # Test -json=<field> with multiple fields.
21 go list -json=ImportPath,Name,GoFiles,Imports
22 cmp stdout want-json-multiple.txt
23
24 # Test -json=<field> with Deps outputs the Deps field.
25 go list -json=Deps
26 stdout '"Deps": \['
27 stdout '"errors",'
28
29 # Test -json=<field> with *EmbedPatterns outputs embed patterns.
30 cd embed
31 go list -json=EmbedPatterns,TestEmbedPatterns,XTestEmbedPatterns
32 stdout '"EmbedPatterns": \['
33 stdout '"TestEmbedPatterns": \['
34 stdout '"XTestEmbedPatterns": \['
35 # Test -json=<field> with *EmbedFiles fails due to broken file reference.
36 ! go list -json=EmbedFiles
37 stderr 'no matching files found'
38 ! go list -json=TestEmbedFiles
39 stderr 'no matching files found'
40 ! go list -json=XTestEmbedFiles
41 stderr 'no matching files found'
42 cd ..
43
44 [!git] skip
45
46 # Test -json=<field> without Stale skips computing buildinfo
47 cd repo
48 exec git init
49 # Control case: with -json=Stale cmd/go executes git status to compute buildinfo
50 go list -json=Stale -x
51 stderr 'git status'
52 # Test case: without -json=Stale cmd/go skips git status
53 go list -json=Name -x
54 ! stderr 'git status'
55
56 -- go.mod --
57 module example.com/a
58
59 go 1.18
60 -- a.go --
61 package a
62
63 import "fmt"
64
65 func F() {
66 fmt.Println("hey there")
67 }
68 -- want-non-json.txt --
69 example.com/a
70 -- want-json-name.txt --
71 {
72 "Name": "a"
73 }
74 -- want-json-multiple.txt --
75 {
76 "ImportPath": "example.com/a",
77 "Name": "a",
78 "GoFiles": [
79 "a.go"
80 ],
81 "Imports": [
82 "fmt"
83 ]
84 }
85 -- repo/go.mod --
86 module example.com/repo
87 -- repo/main.go --
88 package main
89
90 func main() {}
91 -- embed/go.mod --
92 module example.com/embed
93 -- embed/embed.go --
94 package embed
95
96 import _ "embed"
97
98 //go:embed non-existing-file.txt
99 var s string
100 -- embed/embed_test.go --
101 package embed
102
103 import _ "embed"
104
105 //go:embed non-existing-file.txt
106 var s string
107 -- embed/embed_xtest_test.go --
108 package embed_test
109
110 import _ "embed"
111
112 //go:embed non-existing-file.txt
113 var s string
114
View as plain text