Skip to content

Commit

Permalink
Add File() and Dir() to validation examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Aug 16, 2024
1 parent 7055eb4 commit e9275a9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion builder_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (b *DirBuilder) WithMustExist() *DirBuilder {
info, err := os.Stat(string(v))
if err != nil {
if os.IsNotExist(err) {
return errors.New("the directory does not exist")
return errors.New("expected the directory to exist")
}
return err
}
Expand Down
2 changes: 1 addition & 1 deletion builder_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var _ = Describe("type DirBuilder", func() {
WithMustExist().
Required().
Value()
}).To(PanicWith("value of FERRITE_DIR (/path/to/dir) is invalid: the directory does not exist"))
}).To(PanicWith("value of FERRITE_DIR (/path/to/dir) is invalid: expected the directory to exist"))
})

It("panics if the path refers to a non-directory", func() {
Expand Down
6 changes: 3 additions & 3 deletions builder_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (b *FileBuilder) WithDefault(v string) *FileBuilder {
return b
}

// WithMustExist adds a constraint that requires the file to already exist when
// the application starts.
// WithMustExist adds a constraint that requires the value to refer to an
// existing file.
func (b *FileBuilder) WithMustExist() *FileBuilder {
b.builder.BuiltInConstraint(
"**MUST** refer to a file that already exists",
Expand All @@ -51,7 +51,7 @@ func (b *FileBuilder) WithMustExist() *FileBuilder {
info, err := os.Stat(string(v))
if err != nil {
if os.IsNotExist(err) {
return errors.New("the file does not exist")
return errors.New("expected the file to exist")
}
return err
}
Expand Down
2 changes: 1 addition & 1 deletion builder_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ var _ = Describe("type FileBuilder", func() {
WithMustExist().
Required().
Value()
}).To(PanicWith("value of FERRITE_FILE (/path/to/file) is invalid: the file does not exist"))
}).To(PanicWith("value of FERRITE_FILE (/path/to/file) is invalid: expected the file to exist"))
})

It("panics if the path refers to a directory", func() {
Expand Down
48 changes: 48 additions & 0 deletions mode_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func ExampleInit_validation() {
Bool("FERRITE_BOOL", "example bool").
Required()

os.Setenv("FERRITE_DIR", "/path/to/dir")
ferrite.
Dir("FERRITE_DIR", "example dir").
Required()

os.Setenv("FERRITE_DURATION", "3h20m")
ferrite.
Duration("FERRITE_DURATION", "example duration").
Expand All @@ -38,6 +43,11 @@ func ExampleInit_validation() {
WithMembers("foo", "bar", "baz").
Required()

os.Setenv("FERRITE_FILE", "/path/to/file")
ferrite.
File("FERRITE_FILE", "example file").
Required()

os.Setenv("FERRITE_NETWORK_PORT", "8080")
ferrite.
NetworkPort("FERRITE_NETWORK_PORT", "example network port").
Expand Down Expand Up @@ -92,8 +102,10 @@ func ExampleInit_validation() {
// FERRITE_BINARY example binary <base64> ✓ set to {12 bytes}
// FERRITE_BINARY_SENSITIVE example sensitive binary <base64> ✓ set to {12 bytes}
// FERRITE_BOOL example bool true | false ✓ set to true
// FERRITE_DIR example dir <string> ✓ set to /path/to/dir
// FERRITE_DURATION example duration 1ns ... ✓ set to 3h20m
// FERRITE_ENUM example enum foo | bar | baz ✓ set to foo
// FERRITE_FILE example file <string> ✓ set to /path/to/file
// FERRITE_NETWORK_PORT example network port <string> ✓ set to 8080
// FERRITE_NUM_FLOAT example float-point <float32> ✓ set to -123.45
// FERRITE_NUM_SIGNED example signed integer <int16> ✓ set to -123
Expand Down Expand Up @@ -127,6 +139,11 @@ func ExampleInit_validationWithDefaultValues() {
WithDefault(true).
Required()

ferrite.
Dir("FERRITE_DIR", "example dir").
WithDefault("/path/to/dir").
Required()

ferrite.
Duration("FERRITE_DURATION", "example duration").
WithDefault(10 * time.Second).
Expand All @@ -138,6 +155,11 @@ func ExampleInit_validationWithDefaultValues() {
WithDefault("bar").
Required()

ferrite.
File("FERRITE_FILE", "example file").
WithDefault("/path/to/file").
Required()

ferrite.
NetworkPort("FERRITE_NETWORK_PORT", "example network port").
WithDefault("8080").
Expand Down Expand Up @@ -191,8 +213,10 @@ func ExampleInit_validationWithDefaultValues() {
// FERRITE_BINARY example binary [ <base64> ] = {12 bytes} ✓ using default value
// FERRITE_BINARY_SENSITIVE example sensitive binary [ <base64> ] = {12 bytes} ✓ using default value
// FERRITE_BOOL example bool [ true | false ] = true ✓ using default value
// FERRITE_DIR example dir [ <string> ] = /path/to/dir ✓ using default value
// FERRITE_DURATION example duration [ 1ns ... ] = 10s ✓ using default value
// FERRITE_ENUM example enum [ foo | bar | baz ] = bar ✓ using default value
// FERRITE_FILE example file [ <string> ] = /path/to/file ✓ using default value
// FERRITE_NETWORK_PORT example network port [ <string> ] = 8080 ✓ using default value
// FERRITE_NUM_FLOAT example float-point [ <float32> ] = -123.45 ✓ using default value
// FERRITE_NUM_SIGNED example signed integer [ <int16> ] = -123 ✓ using default value
Expand Down Expand Up @@ -223,6 +247,10 @@ func ExampleInit_validationWithOptionalValues() {
Bool("FERRITE_BOOL", "example bool").
Optional()

ferrite.
Dir("FERRITE_DIR", "example dir").
Optional()

ferrite.
Duration("FERRITE_DURATION", "example duration").
Optional()
Expand All @@ -232,6 +260,10 @@ func ExampleInit_validationWithOptionalValues() {
WithMembers("foo", "bar", "baz").
Optional()

ferrite.
File("FERRITE_FILE", "example file").
Optional()

ferrite.
NetworkPort("FERRITE_NETWORK_PORT", "example network port").
Optional()
Expand Down Expand Up @@ -277,8 +309,10 @@ func ExampleInit_validationWithOptionalValues() {
// FERRITE_BINARY example binary [ <base64> ] • undefined
// FERRITE_BINARY_SENSITIVE example sensitive binary [ <base64> ] • undefined
// FERRITE_BOOL example bool [ true | false ] • undefined
// FERRITE_DIR example dir [ <string> ] • undefined
// FERRITE_DURATION example duration [ 1ns ... ] • undefined
// FERRITE_ENUM example enum [ foo | bar | baz ] • undefined
// FERRITE_FILE example file [ <string> ] • undefined
// FERRITE_NETWORK_PORT example network port [ <string> ] • undefined
// FERRITE_NUM_FLOAT example float-point [ <float32> ] • undefined
// FERRITE_NUM_SIGNED example signed integer [ <int16> ] • undefined
Expand Down Expand Up @@ -335,6 +369,12 @@ func ExampleInit_validationWithInvalidValues() {
Bool("FERRITE_BOOL", "example bool").
Required()

os.Setenv("FERRITE_DIR", "/path/to/dir")
ferrite.
Dir("FERRITE_DIR", "example dir").
WithMustExist().
Required()

os.Setenv("FERRITE_DURATION", "-+10s")
ferrite.
Duration("FERRITE_DURATION", "example duration").
Expand All @@ -346,6 +386,12 @@ func ExampleInit_validationWithInvalidValues() {
WithMembers("foo", "bar", "baz").
Required()

os.Setenv("FERRITE_FILE", "/path/to/file")
ferrite.
File("FERRITE_FILE", "example file").
WithMustExist().
Required()

os.Setenv("FERRITE_NETWORK_PORT", "<invalid port>")
ferrite.
NetworkPort("FERRITE_NETWORK_PORT", "example network port").
Expand Down Expand Up @@ -408,8 +454,10 @@ func ExampleInit_validationWithInvalidValues() {
// ❯ FERRITE_BINARY example binary <base64> ✗ set to {16 bytes}, illegal base64 data at input byte 0
// ❯ FERRITE_BINARY_SENSITIVE example sensitive binary <base64> ✗ set to {16 bytes}, illegal base64 data at input byte 0
// ❯ FERRITE_BOOL example bool true | false ✗ set to yes, expected either true or false
// ❯ FERRITE_DIR example dir <string> ✗ set to /path/to/dir, expected the directory to exist
// ❯ FERRITE_DURATION example duration 1ns ... ✗ set to -+10s, expected duration
// ❯ FERRITE_ENUM example enum foo | bar | baz ✗ set to qux, expected foo, bar or baz
// ❯ FERRITE_FILE example file <string> ✗ set to /path/to/file, expected the file to exist
// ❯ FERRITE_NETWORK_PORT example network port <string> ✗ set to '<invalid port>', IANA service name must contain only ASCII letters, digits and hyphen
// ❯ FERRITE_NUM_FLOAT example float-point <float32> ✗ set to -123w45, expected float32
// ❯ FERRITE_NUM_SIGNED example signed integer <int16> ✗ set to 123.3, expected integer between -32768 and +32767
Expand Down

0 comments on commit e9275a9

Please sign in to comment.