Skip to content

Commit

Permalink
use (testing.T).TempDir instead of tempMkdir
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Mar 6, 2024
1 parent 999f629 commit f566838
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 69 deletions.
26 changes: 9 additions & 17 deletions inotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ func TestInotifyCloseSlightlyLater(t *testing.T) {
}

func TestInotifyCloseSlightlyLaterWithWatch(t *testing.T) {
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

w, err := NewWatcher()
if err != nil {
Expand All @@ -67,8 +66,7 @@ func TestInotifyCloseSlightlyLaterWithWatch(t *testing.T) {
}

func TestInotifyCloseAfterRead(t *testing.T) {
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

w, err := NewWatcher()
if err != nil {
Expand Down Expand Up @@ -113,8 +111,7 @@ func isWatcherReallyClosed(t *testing.T, w *Watcher) {
}

func TestInotifyCloseCreate(t *testing.T) {
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

w, err := NewWatcher()
if err != nil {
Expand Down Expand Up @@ -161,8 +158,7 @@ func TestInotifyCloseCreate(t *testing.T) {
func TestInotifyStress(t *testing.T) {
maxNumToCreate := 1000

testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()
testFilePrefix := filepath.Join(testDir, "testfile")

w, err := NewWatcher()
Expand Down Expand Up @@ -275,8 +271,7 @@ func TestInotifyStress(t *testing.T) {
}

func TestInotifyRemoveTwice(t *testing.T) {
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()
testFile := filepath.Join(testDir, "testfile")

handle, err := os.Create(testFile)
Expand Down Expand Up @@ -319,8 +314,7 @@ func TestInotifyRemoveTwice(t *testing.T) {
}

func TestInotifyInnerMapLength(t *testing.T) {
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()
testFile := filepath.Join(testDir, "testfile")

handle, err := os.Create(testFile)
Expand Down Expand Up @@ -375,8 +369,7 @@ func TestInotifyOverflow(t *testing.T) {
numDirs := 128
numFiles := 1024

testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

w, err := NewWatcher()
if err != nil {
Expand Down Expand Up @@ -464,8 +457,7 @@ func TestInotifyOverflow(t *testing.T) {
}

func TestInotifyWatchList(t *testing.T) {
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()
testFile := filepath.Join(testDir, "testfile")

handle, err := os.Create(testFile)
Expand Down Expand Up @@ -501,7 +493,7 @@ func TestInotifyWatchList(t *testing.T) {
}

func TestInotifyDeleteOpenedFile(t *testing.T) {
testDir := tempMkdir(t)
testDir := t.TempDir()
defer os.RemoveAll(testDir)

testFile := filepath.Join(testDir, "testfile")
Expand Down
4 changes: 2 additions & 2 deletions integration_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func testExchangedataForWatcher(t *testing.T, watchDir bool) {
}

// Create directory to watch
testDir1 := tempMkdir(t)
testDir1 := t.TempDir()

// For the intermediate file
testDir2 := tempMkdir(t)
testDir2 := t.TempDir()

defer os.RemoveAll(testDir1)
defer os.RemoveAll(testDir2)
Expand Down
72 changes: 22 additions & 50 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package fsnotify

import (
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -42,18 +41,10 @@ func (c *counter) reset() {
atomic.StoreInt32(&c.val, 0)
}

// tempMkdir makes a temporary directory
func tempMkdir(t *testing.T) string {
dir, err := ioutil.TempDir("", "fsnotify")
if err != nil {
t.Fatalf("failed to create test directory: %s", err)
}
return dir
}

// tempMkFile makes a temporary file.
func tempMkFile(t *testing.T, dir string) string {
f, err := ioutil.TempFile(dir, "fsnotify")
t.Helper()
f, err := os.CreateTemp(dir, "fsnotify")
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
Expand Down Expand Up @@ -95,12 +86,10 @@ func TestFsnotifyMultipleOperations(t *testing.T) {
}()

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

// Create directory that's not watched
testDirToMoveFiles := tempMkdir(t)
defer os.RemoveAll(testDirToMoveFiles)
testDirToMoveFiles := t.TempDir()

testFile := filepath.Join(testDir, "TestFsnotifySeq.testfile")
testFileRenamed := filepath.Join(testDirToMoveFiles, "TestFsnotifySeqRename.testfile")
Expand Down Expand Up @@ -216,8 +205,7 @@ func TestFsnotifyMultipleCreates(t *testing.T) {
}()

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

testFile := filepath.Join(testDir, "TestFsnotifySeq.testfile")

Expand Down Expand Up @@ -335,8 +323,7 @@ func TestFsnotifyDirOnly(t *testing.T) {
watcher := newWatcher(t)

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

// Create a file before watching directory
// This should NOT add any events to the fsnotify event queue
Expand Down Expand Up @@ -438,8 +425,7 @@ func TestFsnotifyDeleteWatchedDir(t *testing.T) {
defer watcher.Close()

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

// Create a file before watching directory
testFileAlreadyExists := filepath.Join(testDir, "TestFsnotifyEventsExisting.testfile")
Expand Down Expand Up @@ -496,8 +482,7 @@ func TestFsnotifySubDir(t *testing.T) {
watcher := newWatcher(t)

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

testFile1 := filepath.Join(testDir, "TestFsnotifyFile1.testfile")
testSubDir := filepath.Join(testDir, "sub")
Expand Down Expand Up @@ -590,8 +575,7 @@ func TestFsnotifyRename(t *testing.T) {
watcher := newWatcher(t)

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

addWatch(t, watcher, testDir)

Expand Down Expand Up @@ -668,12 +652,10 @@ func TestFsnotifyRenameToCreate(t *testing.T) {
watcher := newWatcher(t)

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

// Create directory to get file
testDirFrom := tempMkdir(t)
defer os.RemoveAll(testDirFrom)
testDirFrom := t.TempDir()

addWatch(t, watcher, testDir)

Expand Down Expand Up @@ -749,12 +731,10 @@ func TestFsnotifyRenameToOverwrite(t *testing.T) {
watcher := newWatcher(t)

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

// Create directory to get file
testDirFrom := tempMkdir(t)
defer os.RemoveAll(testDirFrom)
testDirFrom := t.TempDir()

testFile := filepath.Join(testDirFrom, "TestFsnotifyEvents.testfile")
testFileRenamed := filepath.Join(testDir, "TestFsnotifyEvents.testfileRenamed")
Expand Down Expand Up @@ -830,8 +810,7 @@ func TestFsnotifyRenameToOverwrite(t *testing.T) {

func TestRemovalOfWatch(t *testing.T) {
// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

// Create a file before watching directory
testFileAlreadyExists := filepath.Join(testDir, "TestFsnotifyEventsExisting.testfile")
Expand Down Expand Up @@ -890,8 +869,7 @@ func TestFsnotifyAttrib(t *testing.T) {
watcher := newWatcher(t)

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

// Receive errors on the error channel on a separate goroutine
go func() {
Expand Down Expand Up @@ -1026,8 +1004,7 @@ func TestFsnotifyClose(t *testing.T) {
t.Fatal("double Close() test failed: second Close() call didn't return")
}

testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

if err := watcher.Add(testDir); err == nil {
t.Fatal("expected error on Watch() after Close(), got nil")
Expand All @@ -1042,8 +1019,7 @@ func TestFsnotifyFakeSymlink(t *testing.T) {
watcher := newWatcher(t)

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

var errorsReceived counter
// Receive errors on the error channel on a separate goroutine
Expand Down Expand Up @@ -1105,8 +1081,7 @@ func TestCyclicSymlink(t *testing.T) {

watcher := newWatcher(t)

testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

link := path.Join(testDir, "link")
if err := os.Symlink(".", link); err != nil {
Expand All @@ -1131,7 +1106,7 @@ func TestCyclicSymlink(t *testing.T) {
// no way for us to get events on symlinks themselves, because opening them
// opens an fd to the file to which they point.

if err := ioutil.WriteFile(link, []byte("foo"), 0700); err != nil {
if err := os.WriteFile(link, []byte("foo"), 0700); err != nil {
t.Fatalf("could not make symlink: %v", err)
}

Expand All @@ -1154,8 +1129,7 @@ func TestConcurrentRemovalOfWatch(t *testing.T) {
}

// Create directory to watch
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

// Create a file before watching directory
testFileAlreadyExists := filepath.Join(testDir, "TestFsnotifyEventsExisting.testfile")
Expand Down Expand Up @@ -1191,8 +1165,7 @@ func TestConcurrentRemovalOfWatch(t *testing.T) {

func TestClose(t *testing.T) {
// Regression test for #59 bad file descriptor from Close
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

watcher := newWatcher(t)
if err := watcher.Add(testDir); err != nil {
Expand All @@ -1207,8 +1180,7 @@ func TestClose(t *testing.T) {
// TestRemoveWithClose tests if one can handle Remove events and, at the same
// time, close Watcher object without any data races.
func TestRemoveWithClose(t *testing.T) {
testDir := tempMkdir(t)
defer os.RemoveAll(testDir)
testDir := t.TempDir()

const fileN = 200
tempFiles := make([]string, 0, fileN)
Expand Down

0 comments on commit f566838

Please sign in to comment.