change temp dir to os.TempDir for package gfile (#2547)

This commit is contained in:
John Guo
2023-03-29 11:58:20 +08:00
committed by GitHub
parent 4af9ce8a81
commit 6a4e39e815
3 changed files with 8 additions and 22 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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())
})
}