-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Feat: uds_exists validator #1482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Feat: uds_exists validator #1482
Conversation
89dc794 to
94e0bfc
Compare
94e0bfc to
4b86d5b
Compare
zemzale
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like the tests to be cleaner, but otherwise looks good
| errs := validate.Var("", "uds_exists") | ||
| NotEqual(t, errs, nil) | ||
| AssertError(t, errs, "", "", "", "", "uds_exists") | ||
|
|
||
| errs = validate.Var("/tmp/nonexistent.sock", "uds_exists") | ||
| NotEqual(t, errs, nil) | ||
| AssertError(t, errs, "", "", "", "", "uds_exists") | ||
|
|
||
| sockPath := "/tmp/test_validator.sock" | ||
| var lc net.ListenConfig | ||
| listener, err := lc.Listen(t.Context(), "unix", sockPath) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create test socket: %v", err) | ||
| } | ||
| defer func() { | ||
| _ = listener.Close() | ||
| }() | ||
| defer func() { | ||
| _ = os.Remove(sockPath) | ||
| }() | ||
| errs = validate.Var(sockPath, "uds_exists") | ||
| Equal(t, errs, nil) | ||
|
|
||
| regularFile := "/tmp/test_validator_regular.txt" | ||
| if err := os.WriteFile(regularFile, []byte("test"), 0644); err != nil { | ||
| t.Fatalf("Failed to create regular file: %v", err) | ||
| } | ||
| defer func() { | ||
| _ = os.Remove(regularFile) | ||
| }() | ||
| errs = validate.Var(regularFile, "uds_exists") | ||
| NotEqual(t, errs, nil) | ||
| AssertError(t, errs, "", "", "", "", "uds_exists") | ||
|
|
||
| dirPath := "/tmp/test_validator_dir" | ||
| if err := os.Mkdir(dirPath, 0755); err != nil && !os.IsExist(err) { | ||
| t.Fatalf("Failed to create directory: %v", err) | ||
| } | ||
| defer func() { | ||
| _ = os.RemoveAll(dirPath) | ||
| }() | ||
| errs = validate.Var(dirPath, "uds_exists") | ||
| NotEqual(t, errs, nil) | ||
| AssertError(t, errs, "", "", "", "", "uds_exists") | ||
|
|
||
| if runtime.GOOS == "linux" { | ||
| abstractSockName := "@test_abstract_socket_" + fmt.Sprintf("%d", time.Now().UnixNano()) | ||
| var lc net.ListenConfig | ||
| abstractListener, err := lc.Listen(t.Context(), "unix", "\x00"+abstractSockName[1:]) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create abstract socket: %v", err) | ||
| } | ||
| defer func() { | ||
| _ = abstractListener.Close() | ||
| }() | ||
| errs = validate.Var(abstractSockName, "uds_exists") | ||
| Equal(t, errs, nil) | ||
| errs = validate.Var("@nonexistent_abstract_socket", "uds_exists") | ||
| NotEqual(t, errs, nil) | ||
| AssertError(t, errs, "", "", "", "", "uds_exists") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only issue I have here, is that I would want the tests to be split up and ran as sub tests.
t.Run("non_existant", func(t *testing.T) {
errs = validate.Var("/tmp/nonexistent.sock", "uds_exists")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "uds_exists")
})Otherwise with all the defer and other things feel hard to read.
Fixes Or Enhances
Make sure that you've checked the boxes below before you submit PR:
@go-playground/validator-maintainers
As discussed in: #1348