From 8a9bf5ab7bd4e02ef437864f211af835544d80b4 Mon Sep 17 00:00:00 2001 From: noys Date: Sun, 7 Apr 2024 12:34:19 +0300 Subject: [PATCH 1/6] :bug: Now multiple programs or goroutines calling MkdirTemp simultaneously will not choose the same directory --- utils/fileutils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/fileutils.go b/utils/fileutils.go index b21cc9be..804f846e 100644 --- a/utils/fileutils.go +++ b/utils/fileutils.go @@ -273,7 +273,7 @@ func GetFileContentAndInfo(filePath string) (fileContent []byte, fileInfo os.Fil func CreateTempDir() (string, error) { tempDirBase := os.TempDir() timestamp := strconv.FormatInt(time.Now().Unix(), 10) - return os.MkdirTemp(tempDirBase, tempDirPrefix+timestamp+"-") + return os.MkdirTemp(tempDirBase, tempDirPrefix+timestamp+"*") } func RemoveTempDir(dirPath string) error { From 0e247bdd56eec094c557deab7c2c7e9aa9f476e8 Mon Sep 17 00:00:00 2001 From: noys Date: Sun, 7 Apr 2024 15:47:19 +0300 Subject: [PATCH 2/6] :construction: --- utils/fileutils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/fileutils.go b/utils/fileutils.go index 804f846e..b21cc9be 100644 --- a/utils/fileutils.go +++ b/utils/fileutils.go @@ -273,7 +273,7 @@ func GetFileContentAndInfo(filePath string) (fileContent []byte, fileInfo os.Fil func CreateTempDir() (string, error) { tempDirBase := os.TempDir() timestamp := strconv.FormatInt(time.Now().Unix(), 10) - return os.MkdirTemp(tempDirBase, tempDirPrefix+timestamp+"*") + return os.MkdirTemp(tempDirBase, tempDirPrefix+timestamp+"-") } func RemoveTempDir(dirPath string) error { From ab49c71c233dffc9da94e9ddf269344fe5e0e320 Mon Sep 17 00:00:00 2001 From: noys Date: Sun, 7 Apr 2024 16:04:10 +0300 Subject: [PATCH 3/6] :construction: --- utils/fileutils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/fileutils.go b/utils/fileutils.go index b21cc9be..61d766ac 100644 --- a/utils/fileutils.go +++ b/utils/fileutils.go @@ -273,7 +273,7 @@ func GetFileContentAndInfo(filePath string) (fileContent []byte, fileInfo os.Fil func CreateTempDir() (string, error) { tempDirBase := os.TempDir() timestamp := strconv.FormatInt(time.Now().Unix(), 10) - return os.MkdirTemp(tempDirBase, tempDirPrefix+timestamp+"-") + return os.MkdirTemp(tempDirBase, tempDirPrefix+timestamp+"-*") } func RemoveTempDir(dirPath string) error { From 22e5a4721c9594e2c2cba0a9f3967e6991bbf541 Mon Sep 17 00:00:00 2001 From: noys Date: Sun, 7 Apr 2024 17:13:24 +0300 Subject: [PATCH 4/6] :white_check_mark: test added --- utils/fileutils_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/utils/fileutils_test.go b/utils/fileutils_test.go index 3cede3c2..b50a3b7e 100644 --- a/utils/fileutils_test.go +++ b/utils/fileutils_test.go @@ -54,3 +54,17 @@ func TestReadNLines(t *testing.T) { assert.True(t, strings.HasPrefix(lines[1], "781")) assert.True(t, strings.HasSuffix(lines[1], ":true}}}")) } + +func TestCreateTempDir(t *testing.T) { + tempDir, err := CreateTempDir() + assert.NoError(t, err) + + _, err = os.Stat(tempDir) + assert.NotErrorIs(t, err, os.ErrNotExist) + + // Check that a timestamp can be extracted from the temp dir name + _, err = extractTimestamp(tempDir) + assert.NoError(t, err) + + assert.NoError(t, os.RemoveAll(tempDir)) +} \ No newline at end of file From f9c73d3392c724478f34d468ce538c7fbab9d3cb Mon Sep 17 00:00:00 2001 From: noys Date: Sun, 7 Apr 2024 17:39:09 +0300 Subject: [PATCH 5/6] :art: --- utils/fileutils_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/utils/fileutils_test.go b/utils/fileutils_test.go index b50a3b7e..fc6326c7 100644 --- a/utils/fileutils_test.go +++ b/utils/fileutils_test.go @@ -62,9 +62,11 @@ func TestCreateTempDir(t *testing.T) { _, err = os.Stat(tempDir) assert.NotErrorIs(t, err, os.ErrNotExist) + defer func() { // Check that a timestamp can be extracted from the temp dir name _, err = extractTimestamp(tempDir) assert.NoError(t, err) - assert.NoError(t, os.RemoveAll(tempDir)) + assert.NoError(t, os.RemoveAll(tempDir)) + }() } \ No newline at end of file From a9338c9501eefc8bf970459eb7ef56628ffc77fc Mon Sep 17 00:00:00 2001 From: noys Date: Sun, 7 Apr 2024 17:52:51 +0300 Subject: [PATCH 6/6] :art: --- utils/fileutils_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/utils/fileutils_test.go b/utils/fileutils_test.go index fc6326c7..556af447 100644 --- a/utils/fileutils_test.go +++ b/utils/fileutils_test.go @@ -56,17 +56,17 @@ func TestReadNLines(t *testing.T) { } func TestCreateTempDir(t *testing.T) { - tempDir, err := CreateTempDir() - assert.NoError(t, err) + tempDir, err := CreateTempDir() + assert.NoError(t, err) - _, err = os.Stat(tempDir) + _, err = os.Stat(tempDir) assert.NotErrorIs(t, err, os.ErrNotExist) defer func() { - // Check that a timestamp can be extracted from the temp dir name - _, err = extractTimestamp(tempDir) - assert.NoError(t, err) + // Check that a timestamp can be extracted from the temp dir name + _, err = extractTimestamp(tempDir) + assert.NoError(t, err) - assert.NoError(t, os.RemoveAll(tempDir)) + assert.NoError(t, os.RemoveAll(tempDir)) }() -} \ No newline at end of file +}