From 6a4e39e815900f12cf29f1b53bef311f86bf3f59 Mon Sep 17 00:00:00 2001 From: John Guo Date: Wed, 29 Mar 2023 11:58:20 +0800 Subject: [PATCH] change temp dir to os.TempDir for package gfile (#2547) --- os/gfile/gfile.go | 13 ++----------- os/gfile/gfile_z_exmaple_basic_test.go | 11 +++++------ os/gfile/gfile_z_unit_test.go | 6 +----- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/os/gfile/gfile.go b/os/gfile/gfile.go index f3a87381e..bdb5aeac9 100644 --- a/os/gfile/gfile.go +++ b/os/gfile/gfile.go @@ -11,7 +11,6 @@ import ( "os" "os/exec" "path/filepath" - "runtime" "strings" "time" @@ -42,16 +41,9 @@ var ( // selfPath is the current running binary path. // As it is most commonly used, it is so defined as an internal package variable. selfPath = "" - - // Temporary directory of system. - tempDir = "/tmp" ) func init() { - // Initialize internal package variable: tempDir. - if runtime.GOOS == "windows" || Separator != "/" || !Exists(tempDir) { - tempDir = os.TempDir() - } // Initialize internal package variable: selfPath. selfPath, _ = exec.LookPath(os.Args[0]) if selfPath != "" { @@ -445,14 +437,13 @@ func ExtName(path string) string { } // Temp retrieves and returns the temporary directory of current system. -// It returns "/tmp" is current in *nix system, or else it returns os.TempDir(). // // The optional parameter `names` specifies the sub-folders/sub-files, // which will be joined with current system separator and returned with the path. func Temp(names ...string) string { - path := tempDir + path := os.TempDir() for _, name := range names { - path += Separator + name + path = Join(path, name) } return path } diff --git a/os/gfile/gfile_z_exmaple_basic_test.go b/os/gfile/gfile_z_exmaple_basic_test.go index 3f9d87ded..dc658f70b 100644 --- a/os/gfile/gfile_z_exmaple_basic_test.go +++ b/os/gfile/gfile_z_exmaple_basic_test.go @@ -147,7 +147,7 @@ func ExampleJoin() { fmt.Println(joinString) - // Output: + // May Output: // /tmp/gfile_example_basic_dir/file1 } @@ -389,7 +389,7 @@ func ExampleAbs() { // Get an absolute representation of path. fmt.Println(gfile.Abs(path)) - // Output: + // May Output: // /tmp/gfile_example_basic_dir/file1 } @@ -404,9 +404,8 @@ func ExampleRealPath() { fmt.Println(gfile.RealPath(realPath)) fmt.Println(gfile.RealPath(worryPath)) - // Output: + // May Output: // /tmp/gfile_example_basic_dir/file1 - // } func ExampleSelfPath() { @@ -471,7 +470,7 @@ func ExampleDir() { // Get all but the last element of path, typically the path's directory. fmt.Println(gfile.Dir(path)) - // Output: + // May Output: // /tmp/gfile_example_basic_dir } @@ -532,7 +531,7 @@ func ExampleTempDir() { fmt.Println(path) - // Output: + // May Output: // /tmp/gfile_example_basic_dir } diff --git a/os/gfile/gfile_z_unit_test.go b/os/gfile/gfile_z_unit_test.go index 8b56384da..4760ce1ab 100644 --- a/os/gfile/gfile_z_unit_test.go +++ b/os/gfile/gfile_z_unit_test.go @@ -619,11 +619,7 @@ func Test_ExtName(t *testing.T) { func Test_TempDir(t *testing.T) { gtest.C(t, func(t *gtest.T) { - if gfile.Separator != "/" || !gfile.Exists("/tmp") { - t.Assert(gfile.Temp(), os.TempDir()) - } else { - t.Assert(gfile.Temp(), "/tmp") - } + t.Assert(gfile.Temp(), os.TempDir()) }) }