Skip to content
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

test: add unit tests for content parsing and input reading #17

Merged
merged 1 commit into from
Jun 25, 2024

Conversation

supitsdu
Copy link
Owner

  • Add tests for copyToClipboard function to verify clipboard operations.
  • Add tests for readFromStdin function to ensure proper stdin reading.
  • Add tests for readFromFile function to verify file reading functionality.
  • Add tests for parseContent function to check parsing of direct text input, stdin, and file inputs.

These tests enhance the robustness of the application by ensuring all core functionalities are working as expected.

- Add tests for `copyToClipboard` function to verify clipboard operations.
- Add tests for `readFromStdin` function to ensure proper stdin reading.
- Add tests for `readFromFile` function to verify file reading functionality.
- Add tests for `parseContent` function to check parsing of direct text input, stdin, and file inputs.

These tests enhance the robustness of the application by ensuring all core functionalities are working as expected.
@supitsdu supitsdu added the testing Issues or PRs specifically related to Go testing frameworks or practices. label Jun 25, 2024
@supitsdu supitsdu merged commit 0beb06d into main Jun 25, 2024
1 check passed
@supitsdu supitsdu deleted the feature/tests branch June 25, 2024 03:53
@ccoVeille
Copy link
Contributor

I love you added tests

@supitsdu
Copy link
Owner Author

I love you added tests

Thanks a ton for your help with parseContent! Glad you love the new tests.

Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My philosophy in code review and programming could be sum up with Give a Man a Fish, and You Feed Him for a Day. Teach a Man To Fish, and You Feed Him for a Lifetime

}

// Helper function to create temporary file for testing
func createTempFile(content string) *os.File {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a safer and cleaner way to do

Suggested change
func createTempFile(content string) *os.File {
func createTempFile(t *testing.T, content string) *os.File {
file, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fail("creating temporary test file")
}
// register the cleaner without having a need to handle the file removal with a defer that can be forgotten
t.Cleanup (func() {
defer os.Remove(file.Name())
})
file.WriteString(content)
file.Close()
return file
}

If you have a look at os.CreateTemp code

https://cs.opensource.google/go/go/+/go1.22.4:src/os/tempfile.go;l=33

You will see the code is using tempdir when using "" as empty folder.

So you are creating a folder, but nothing delete it when the test are over.

Then, now you have a better understanding, there is the better way to write it

Suggested change
func createTempFile(content string) *os.File {
func createTempFile(t *testing.T, content string) *os.File {
file, err := os.CreateTemp(t.TempDir(), "testfile")
if err != nil {
t.Fail("creating temporary test file", err)
}
file.WriteString(content)
file.Close()
return file
}

Please have look at https://pkg.go.dev/testing#T.TempDir

and its implementation https://cs.opensource.google/go/go/+/refs/tags/go1.22.4:src/testing/testing.go;l=1229

No need for cleaning up the file, then because the whole temp folder is nuked by the cleanup added via t.TempDir

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a safer and cleaner way to do

I opened an issue in an existing linter to report the use case I discovered here


func TestParseContentStdin(t *testing.T) {
stdinInput := "Stdin input\n"
r, w, _ := os.Pipe()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The very same way you created createTempFile, could do this

Suggested change
r, w, _ := os.Pipe()
function replaceArgs(t *testing.T) (r *os.File, w *os.File)
r, w, err := os.Pipe()
if err != nil {
t.Fail(err)
}
originalStdin := os.Stdin
t.Cleanup (func() {
os.Stdin = originalStdin
r.Close()
w.Close()
})
os.Stdin = r

This way you can reuse it safely everywhere

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked for code review on slack.

Someone suggested using testscript

Someone made a proof of concept.

It allows to do not overload os.Std*

https://github.com/datacharmer/clipper/blob/add-testscript/cli%2Fclipper_test.go

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @datacharmer, BTW

main_test.go Show resolved Hide resolved

func TestParseContentFromFile(t *testing.T) {
contentFromFile := "Content from file"
fakeFile := createTempFile(contentFromFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fakeFile := createTempFile(contentFromFile)
fakeFile := createTempFile(t, contentFromFile)

Please have a look at #17 (comment)

Then the defer can be removed

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always nice to have feedbacks like these, constructive and straightforward. This reviews seems to add a proper resource cleanup and error handling, making the test suite safer and easier to maintain. Loved it!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are welcome, I'm glad you like my feedbacks

main_test.go Show resolved Hide resolved
@ccoVeille
Copy link
Contributor

For records, archive, and a better understanding, this PR adds tests for methods added with #16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing Issues or PRs specifically related to Go testing frameworks or practices.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants