-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
rivertype.JobStates
with full list of job states
Requested in #296, add a new `JobStates()` function that contains a complete list of all `JobState*` constant values, which may be useful in places like testing. Also add a test that does a basic parsing of the corresponding Go file, look for all `JobState*` values, and makes sure that `JobStates()` contains every possible value to prevent future regressions. Fixes #296.
- Loading branch information
Showing
8 changed files
with
118 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
module github.com/riverqueue/river/rivertype | ||
|
||
go 1.21.4 | ||
|
||
require github.com/stretchr/testify v1.9.0 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package rivertype_test | ||
|
||
import ( | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/riverqueue/river/rivertype" | ||
) | ||
|
||
func TestJobStates(t *testing.T) { | ||
t.Parallel() | ||
|
||
jobStates := rivertype.JobStates() | ||
|
||
// One easy check that doesn't require the source file reading below. | ||
require.Contains(t, jobStates, rivertype.JobStateAvailable) | ||
|
||
// Get all job state names from the corresponding source file and make sure | ||
// they're included in JobStates. Helps check that we didn't add a new value | ||
// but forgot to add it to the full list of constant values. | ||
for _, nameAndValue := range allValuesForStringConstantType(t, "river_type.go", "JobState") { | ||
t.Logf("Checking for job state: %s / %s", nameAndValue.Name, nameAndValue.Value) | ||
require.Contains(t, jobStates, rivertype.JobState(nameAndValue.Value)) | ||
} | ||
} | ||
|
||
// stringConstantNameAndValue is a name and value for a string constant like | ||
// `JobStateAvailable` + `available`. | ||
type stringConstantNameAndValue struct{ Name, Value string } | ||
|
||
// allValuesForStringConstantType reads a Go source file and looks for all | ||
// values for the named string constant. | ||
func allValuesForStringConstantType(t *testing.T, srcFile, typeName string) []stringConstantNameAndValue { | ||
t.Helper() | ||
|
||
fset := token.NewFileSet() | ||
|
||
src, err := os.ReadFile(srcFile) | ||
require.NoError(t, err) | ||
|
||
f, err := parser.ParseFile(fset, srcFile, src, parser.ParseComments) | ||
require.NoError(t, err) | ||
|
||
var valueNames []stringConstantNameAndValue | ||
|
||
for _, decl := range f.Decls { | ||
if gen, ok := decl.(*ast.GenDecl); ok && gen.Tok == token.CONST { | ||
for _, spec := range gen.Specs { | ||
// Always ast.ValueSpec for token.CONST. | ||
valueSpec := spec.(*ast.ValueSpec) //nolint:forcetypeassert | ||
|
||
typeIdent, ok := valueSpec.Type.(*ast.Ident) | ||
if !ok || typeIdent.Name != typeName { | ||
continue | ||
} | ||
|
||
for i, nameIdent := range valueSpec.Names { | ||
// Force type assert because we expect one of our constants | ||
// to be defined as a basic type literal like this. | ||
basicLitExpr := valueSpec.Values[i].(*ast.BasicLit) //nolint:forcetypeassert | ||
|
||
valueNames = append(valueNames, stringConstantNameAndValue{ | ||
Name: nameIdent.Name, | ||
Value: basicLitExpr.Value[1 : len(basicLitExpr.Value)-1], // strip quote on either side | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
if len(valueNames) < 1 { | ||
require.FailNow(t, "Not values found", "No values found for source file and constant type: %s / %s", srcFile, typeName) | ||
} | ||
|
||
return valueNames | ||
} |