Skip to content

test: use T.TempDir to create temporary test directory #6710

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

Merged
merged 23 commits into from
Aug 24, 2022
Merged

test: use T.TempDir to create temporary test directory #6710

merged 23 commits into from
Aug 24, 2022

Conversation

Juneezee
Copy link
Contributor

@Juneezee Juneezee commented Jul 7, 2022

Change Description

A testing cleanup.

This pull request replaces ioutil.TempDir with t.TempDir. We can use the T.TempDir function from the testing package to create temporary directory. The directory created by T.TempDir is automatically removed when the test and all its subtests complete.

This saves us at least 2 lines (error check, and cleanup) on every instance, or in some cases adds cleanup that we forgot.

Reference: https://pkg.go.dev/testing#T.TempDir

func TestFoo(t *testing.T) {
	// before
	tmpDir, err := ioutil.TempDir("", "")
	require.NoError(t, err)
	defer os.RemoveAll(tmpDir)

	// now
	tmpDir := t.TempDir()
}

Steps to Test

Changes to existing tests only. The existing testing targets in Makefile and CI cover the changes.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

@guggero guggero added code health Related to code commenting, refactoring, and other non-behaviour improvements tests labels Jul 7, 2022
@guggero guggero added this to the v0.16.0 milestone Jul 7, 2022
@Juneezee
Copy link
Contributor Author

Juneezee commented Jul 8, 2022

Rebased onto master and resolved conflicts.

@Juneezee
Copy link
Contributor Author

Rebased onto master.

@guggero guggero self-requested a review August 15, 2022 07:42
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the cleanup PR!
I think it makes a lot of sense to make use of this new feature.

To make review easier, would it be possible for you to split this PR into multiple commits, one for each top level package (aliasmgr, cert, chainntnfs, chanbackup, channeldb, ...) and prefix the commits accordingly?

Also an entry in the 0.16 change log file under the "Code health" section (you can create it if it doesn't exist yet) would be nice.

@Juneezee Juneezee requested a review from guggero August 15, 2022 13:14
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Wow, that was quick! Thanks a lot for the updates.

Looks very good to me. Did a quick search after checking out this PR and found 7 more instances of ioutil.TempFile throughout the codebase. Do we want to fix those as well?
Since it seems mixing t.TempDir() and ioutil.TempFile() can lead to errors.

Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the updates!
Can you work the last two commits into their corresponding previous commits please? Basically split the commits and squash them with the commit for the package they belong in.
Then I'll do a full review on it again.

@@ -434,7 +433,7 @@ func TestChangeWalletPasswordNewRootkey(t *testing.T) {
// requested.
var tempFiles []string
for i := 0; i < 3; i++ {
file, err := ioutil.TempFile(testDir, "")
file, err := os.CreateTemp(testDir, "")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, this doesn't seem to fix the flaky failure I mentioned before. It looks like you uncovered a problem with the test itself.
We need to add require.NoError(t, unlockMsg.UnloadWallet()) on Line 608 (directly after service.MacResponseChan <- testMac) to fix the flake.

@Juneezee Juneezee requested a review from guggero August 22, 2022 14:10
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Thanks for the blazing fast updates! LGTM 🎉

@guggero guggero requested a review from yyforyongyu August 22, 2022 18:44
Copy link
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

Very nice! I also like bringing the usage of t.Cleanup, which makes everything cleaner. This is an LGTM👍 Meanwhile, could you rebase this PR? We've added an extra build step and kinda wanna see if this PR passes.

@Juneezee Juneezee requested a review from yyforyongyu August 24, 2022 01:06
Copy link
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

LGTM🎉

@guggero guggero merged commit 073d052 into lightningnetwork:master Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code health Related to code commenting, refactoring, and other non-behaviour improvements tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants