...

Text file src/cmd/go/testdata/script/list_json_fields.txt

Documentation: cmd/go/testdata/script

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

View as plain text