mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
change temp dir to os.TempDir for package gfile (#2547)
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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())
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user