|
| 1 | +package rivertype_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/ast" |
| 5 | + "go/parser" |
| 6 | + "go/token" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/riverqueue/river/rivertype" |
| 13 | +) |
| 14 | + |
| 15 | +func TestJobStates(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + jobStates := rivertype.JobStates() |
| 19 | + |
| 20 | + // One easy check that doesn't require the source file reading below. |
| 21 | + require.Contains(t, jobStates, rivertype.JobStateAvailable) |
| 22 | + |
| 23 | + // Get all job state names from the corresponding source file and make sure |
| 24 | + // they're included in JobStates. Helps check that we didn't add a new value |
| 25 | + // but forgot to add it to the full list of constant values. |
| 26 | + for _, nameAndValue := range allValuesForStringConstantType(t, "river_type.go", "JobState") { |
| 27 | + t.Logf("Checking for job state: %s / %s", nameAndValue.Name, nameAndValue.Value) |
| 28 | + require.Contains(t, jobStates, rivertype.JobState(nameAndValue.Value)) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// stringConstantNameAndValue is a name and value for a string constant like |
| 33 | +// `JobStateAvailable` + `available`. |
| 34 | +type stringConstantNameAndValue struct{ Name, Value string } |
| 35 | + |
| 36 | +// allValuesForStringConstantType reads a Go source file and looks for all |
| 37 | +// values for the named string constant. |
| 38 | +func allValuesForStringConstantType(t *testing.T, srcFile, typeName string) []stringConstantNameAndValue { |
| 39 | + t.Helper() |
| 40 | + |
| 41 | + fset := token.NewFileSet() |
| 42 | + |
| 43 | + src, err := os.ReadFile(srcFile) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + f, err := parser.ParseFile(fset, srcFile, src, parser.ParseComments) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + var valueNames []stringConstantNameAndValue |
| 50 | + |
| 51 | + for _, decl := range f.Decls { |
| 52 | + if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.CONST { |
| 53 | + for _, spec := range gen.Specs { |
| 54 | + // Always ast.ValueSpec for token.CONST. |
| 55 | + valueSpec := spec.(*ast.ValueSpec) //nolint:forcetypeassert |
| 56 | + |
| 57 | + typeIdent, ok := valueSpec.Type.(*ast.Ident) |
| 58 | + if !ok || typeIdent.Name != typeName { |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + for i, nameIdent := range valueSpec.Names { |
| 63 | + // Force type assert because we expect one of our constants |
| 64 | + // to be defined as a basic type literal like this. |
| 65 | + basicLitExpr := valueSpec.Values[i].(*ast.BasicLit) //nolint:forcetypeassert |
| 66 | + |
| 67 | + valueNames = append(valueNames, stringConstantNameAndValue{ |
| 68 | + Name: nameIdent.Name, |
| 69 | + Value: basicLitExpr.Value[1 : len(basicLitExpr.Value)-1], // strip quote on either side |
| 70 | + }) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if len(valueNames) < 1 { |
| 77 | + require.FailNow(t, "No values found", "No values found for source file and constant type: %s / %s", srcFile, typeName) |
| 78 | + } |
| 79 | + |
| 80 | + return valueNames |
| 81 | +} |
0 commit comments