verless / verless

A Static Site Generator designed for Markdown-based content with a focus on simplicity and performance.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add tests for fs package

coconutlad opened this issue · comments

The problem the feature solves

Include tests for the fs package so that developers can check if the package works as expected after making changes.
Suggested in #200

Your suggested solution

I would like to work on the solution, which includes -
Unit tests for these functions:

  • CopyFromOS
  • IsSafeToRemove
  • Rmdir
  • StreamFiles

Would writing unit-tests for each function be the best way to test the fs package?
Thank you for your time.

Would writing unit-tests for each function be the best way to test the fs package?

The tests which I already wrote are in most cases Table driven tests, which always test one specific function with several constellations.
-> so yes, if possible write them always for one specific function and also try to mock everything else.
You can try to write them similar to the already existing tests.

As @aligator said, we usually use Table Driven Tests. Some hints:

  • You don't have to test the implementation, but just the results of a function - so your test checks if the tested function returns the expected output for a given input.
  • You don't have to create physical files for tests. Using a afero.MemMapFs is sufficient. For testing StreamFiles, you can either use the files from example or even change the function signature to make it accept a afero.Fs like the other functions.
  • Tests should be independent from each other. For example, TestStreamFiles should only call StreamFiles and not other functions like Rmdir.

I was wondering how to test the IsSafeToRemove function and others without creating files and directories...
Thanks for guidance @aligator and @dominikbraun :)
I'll make sure to keep them independent and in the table driven format.

Is it alright to use this - gotests - to generate boilerplate code for the tests?

Is it alright to use this - gotests - to generate boilerplate code for the tests?

Now that's an interesting tool 😀 It doesn't really matter how you write or generate the tests, just make sure to use our nice test package.

For example, instead of:

if err != nil {
    t.Errorf("error: %s", err.Error())
}

just this:

test.Ok(t, err)

Or instead of:

if number != 2 {
    t.Errorf("expected %v, got %v", 2, number )
}

simply:

test.Equals(t, number , 2)