Skip to content

Commit e8c8b79

Browse files
dmitshurgopherbot
authored andcommitted
cmd/api: remove unused functionality
We no longer use the optional parameter to compareAPI. We now always set allowAdd to false. (Except in tests, making them less useful than they could be.) Flags and parsing their value are no more. Remove all the unused functionality and update test cases so they're closer to what the API checker does when it runs for real. Order the features, required, exception variables and fields more consistently. For golang#43956. Change-Id: Iaa4656a89a3fca3129742165a448d385e55e4a98 Reviewed-on: https://go-review.googlesource.com/c/go/+/489436 Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 0972096 commit e8c8b79

File tree

2 files changed

+48
-76
lines changed

2 files changed

+48
-76
lines changed

src/cmd/api/api.go

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"bufio"
1111
"bytes"
1212
"encoding/json"
13-
"flag"
1413
"fmt"
1514
"go/ast"
1615
"go/build"
@@ -46,8 +45,7 @@ func goCmd() string {
4645
return "go"
4746
}
4847

49-
// contexts are the default contexts which are scanned, unless
50-
// overridden by the -contexts flag.
48+
// contexts are the default contexts which are scanned.
5149
var contexts = []*build.Context{
5250
{GOOS: "linux", GOARCH: "386", CgoEnabled: true},
5351
{GOOS: "linux", GOARCH: "386"},
@@ -96,25 +94,6 @@ func contextName(c *build.Context) string {
9694
return s
9795
}
9896

99-
func parseContext(c string) *build.Context {
100-
parts := strings.Split(c, "-")
101-
if len(parts) < 2 {
102-
log.Fatalf("bad context: %q", c)
103-
}
104-
bc := &build.Context{
105-
GOOS: parts[0],
106-
GOARCH: parts[1],
107-
}
108-
if len(parts) == 3 {
109-
if parts[2] == "cgo" {
110-
bc.CgoEnabled = true
111-
} else {
112-
log.Fatalf("bad context: %q", c)
113-
}
114-
}
115-
return bc
116-
}
117-
11897
var internalPkg = regexp.MustCompile(`(^|/)internal($|/)`)
11998

12099
var exitCode = 0
@@ -152,12 +131,7 @@ func Check(t *testing.T) {
152131

153132
var featureCtx = make(map[string]map[string]bool) // feature -> context name -> true
154133
for _, w := range walkers {
155-
pkgNames := w.stdPackages
156-
if flag.NArg() > 0 {
157-
pkgNames = flag.Args()
158-
}
159-
160-
for _, name := range pkgNames {
134+
for _, name := range w.stdPackages {
161135
pkg, err := w.import_(name)
162136
if _, nogo := err.(*build.NoGoError); nogo {
163137
continue
@@ -193,7 +167,7 @@ func Check(t *testing.T) {
193167
bw := bufio.NewWriter(os.Stdout)
194168
defer bw.Flush()
195169

196-
var required, optional []string
170+
var required []string
197171
for _, file := range checkFiles {
198172
required = append(required, fileFeatures(file, needApproval(file))...)
199173
}
@@ -205,7 +179,7 @@ func Check(t *testing.T) {
205179
if exitCode == 1 {
206180
t.Errorf("API database problems found")
207181
}
208-
if !compareAPI(bw, features, required, optional, exception, false) {
182+
if !compareAPI(bw, features, required, exception) {
209183
t.Errorf("API differences found")
210184
}
211185
}
@@ -251,12 +225,11 @@ func portRemoved(feature string) bool {
251225
strings.Contains(feature, "(darwin-386-cgo)")
252226
}
253227

254-
func compareAPI(w io.Writer, features, required, optional, exception []string, allowAdd bool) (ok bool) {
228+
func compareAPI(w io.Writer, features, required, exception []string) (ok bool) {
255229
ok = true
256230

257-
optionalSet := set(optional)
258-
exceptionSet := set(exception)
259231
featureSet := set(features)
232+
exceptionSet := set(exception)
260233

261234
sort.Strings(features)
262235
sort.Strings(required)
@@ -267,7 +240,7 @@ func compareAPI(w io.Writer, features, required, optional, exception []string, a
267240
return s
268241
}
269242

270-
for len(required) > 0 || len(features) > 0 {
243+
for len(features) > 0 || len(required) > 0 {
271244
switch {
272245
case len(features) == 0 || (len(required) > 0 && required[0] < features[0]):
273246
feature := take(&required)
@@ -288,33 +261,15 @@ func compareAPI(w io.Writer, features, required, optional, exception []string, a
288261
}
289262
case len(required) == 0 || (len(features) > 0 && required[0] > features[0]):
290263
newFeature := take(&features)
291-
if optionalSet[newFeature] {
292-
// Known added feature to the upcoming release.
293-
// Delete it from the map so we can detect any upcoming features
294-
// which were never seen. (so we can clean up the nextFile)
295-
delete(optionalSet, newFeature)
296-
} else {
297-
fmt.Fprintf(w, "+%s\n", newFeature)
298-
if !allowAdd {
299-
ok = false // we're in lock-down mode for next release
300-
}
301-
}
264+
fmt.Fprintf(w, "+%s\n", newFeature)
265+
ok = false // feature not in api/next/*
302266
default:
303267
take(&required)
304268
take(&features)
305269
}
306270
}
307271

308-
// In next file, but not in API.
309-
var missing []string
310-
for feature := range optionalSet {
311-
missing = append(missing, feature)
312-
}
313-
sort.Strings(missing)
314-
for _, feature := range missing {
315-
fmt.Fprintf(w, "±%s\n", feature)
316-
}
317-
return
272+
return ok
318273
}
319274

320275
// aliasReplacer applies type aliases to earlier API files,

src/cmd/api/api_test.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,23 @@ func TestGolden(t *testing.T) {
115115

116116
func TestCompareAPI(t *testing.T) {
117117
tests := []struct {
118-
name string
119-
features, required, optional, exception []string
120-
ok bool // want
121-
out string // want
118+
name string
119+
features, required, exception []string
120+
ok bool // want
121+
out string // want
122122
}{
123+
{
124+
name: "equal",
125+
features: []string{"A", "B", "C"},
126+
required: []string{"A", "B", "C"},
127+
ok: true,
128+
out: "",
129+
},
123130
{
124131
name: "feature added",
125132
features: []string{"A", "B", "C", "D", "E", "F"},
126133
required: []string{"B", "D"},
127-
ok: true,
134+
ok: false,
128135
out: "+A\n+C\n+E\n+F\n",
129136
},
130137
{
@@ -134,42 +141,52 @@ func TestCompareAPI(t *testing.T) {
134141
ok: false,
135142
out: "-B\n",
136143
},
137-
{
138-
name: "feature added then removed",
139-
features: []string{"A", "C"},
140-
optional: []string{"B"},
141-
required: []string{"A", "C"},
142-
ok: true,
143-
out: "±B\n",
144-
},
145144
{
146145
name: "exception removal",
147-
required: []string{"A", "B", "C"},
148146
features: []string{"A", "C"},
147+
required: []string{"A", "B", "C"},
149148
exception: []string{"B"},
150149
ok: true,
151150
out: "",
152151
},
152+
153+
// Test that a feature required on a subset of ports is implicitly satisfied
154+
// by the same feature being implemented on all ports. That is, it shouldn't
155+
// say "pkg syscall (darwin-amd64), type RawSockaddrInet6 struct" is missing.
156+
// See https://go.dev/issue/4303.
153157
{
154-
// https://golang.org/issue/4303
155-
name: "contexts reconverging",
158+
name: "contexts reconverging after api/next/* update",
159+
features: []string{
160+
"A",
161+
"pkg syscall, type RawSockaddrInet6 struct",
162+
},
156163
required: []string{
157164
"A",
158-
"pkg syscall (darwin-amd64), type RawSockaddrInet6 struct",
165+
"pkg syscall (darwin-amd64), type RawSockaddrInet6 struct", // api/go1.n.txt
166+
"pkg syscall, type RawSockaddrInet6 struct", // api/next/n.txt
159167
},
168+
ok: true,
169+
out: "",
170+
},
171+
{
172+
name: "contexts reconverging before api/next/* update",
160173
features: []string{
161174
"A",
162175
"pkg syscall, type RawSockaddrInet6 struct",
163176
},
164-
ok: true,
177+
required: []string{
178+
"A",
179+
"pkg syscall (darwin-amd64), type RawSockaddrInet6 struct",
180+
},
181+
ok: false,
165182
out: "+pkg syscall, type RawSockaddrInet6 struct\n",
166183
},
167184
}
168185
for _, tt := range tests {
169186
buf := new(strings.Builder)
170-
gotok := compareAPI(buf, tt.features, tt.required, tt.optional, tt.exception, true)
171-
if gotok != tt.ok {
172-
t.Errorf("%s: ok = %v; want %v", tt.name, gotok, tt.ok)
187+
gotOK := compareAPI(buf, tt.features, tt.required, tt.exception)
188+
if gotOK != tt.ok {
189+
t.Errorf("%s: ok = %v; want %v", tt.name, gotOK, tt.ok)
173190
}
174191
if got := buf.String(); got != tt.out {
175192
t.Errorf("%s: output differs\nGOT:\n%s\nWANT:\n%s", tt.name, got, tt.out)

0 commit comments

Comments
 (0)