|
| 1 | +package coderdtest_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/ast" |
| 5 | + "go/parser" |
| 6 | + "go/token" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/coder/coder/coderd/coderdtest" |
| 14 | +) |
| 15 | + |
| 16 | +func TestEndpointsDocumented(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + swaggerComments, err := coderdtest.ParseSwaggerComments("..") |
| 20 | + require.NoError(t, err, "can't parse swagger comments") |
| 21 | + |
| 22 | + _, _, api := coderdtest.NewWithAPI(t, nil) |
| 23 | + coderdtest.VerifySwaggerDefinitions(t, api.APIHandler, swaggerComments) |
| 24 | +} |
| 25 | + |
| 26 | +func TestSDKFieldsFormatted(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + fileSet := token.NewFileSet() |
| 30 | + nodes, err := parser.ParseDir(fileSet, "../../codersdk", nil, parser.ParseComments) |
| 31 | + require.NoError(t, err, "parser.ParseDir failed") |
| 32 | + |
| 33 | + for _, node := range nodes { |
| 34 | + ast.Inspect(node, func(n ast.Node) bool { |
| 35 | + typeSpec, ok := n.(*ast.TypeSpec) |
| 36 | + if !ok { |
| 37 | + return true |
| 38 | + } |
| 39 | + structureName := typeSpec.Name |
| 40 | + |
| 41 | + structType, ok := typeSpec.Type.(*ast.StructType) |
| 42 | + if !ok { |
| 43 | + return true // not a structure |
| 44 | + } |
| 45 | + |
| 46 | + for _, field := range structType.Fields.List { |
| 47 | + selectorExpr, ok := field.Type.(*ast.SelectorExpr) |
| 48 | + if !ok { |
| 49 | + continue // rather a basic, or primitive |
| 50 | + } |
| 51 | + |
| 52 | + if field.Tag == nil || !strings.Contains(field.Tag.Value, `json:"`) { |
| 53 | + continue // not a JSON property |
| 54 | + } |
| 55 | + |
| 56 | + switch selectorExpr.Sel.Name { |
| 57 | + case "UUID": |
| 58 | + assert.Contains(t, field.Tag.Value, `format:"uuid"`, `Swagger formatting requires to annotate the field with - format:"uuid". Location: %s/%s`, structureName, field.Names) |
| 59 | + case "Time": |
| 60 | + assert.Contains(t, field.Tag.Value, `format:"date-time"`, `Swagger formatting requires to annotate the field with - format:"date-time". Location: %s/%s`, structureName, field.Names) |
| 61 | + } |
| 62 | + } |
| 63 | + return true |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
0 commit comments