mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 15:36:41 -05:00
Test: add test to avoid the error of missing geoip & geosite (#895)
This commit is contained in:
parent
b9ad33070e
commit
4bbe672ffd
@ -1,6 +1,8 @@
|
|||||||
package platform_test
|
package platform_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -43,6 +45,33 @@ func TestEnvFlag(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestWrongErrorCheckOnOSStat is a test to detect the misuse of error handling
|
||||||
|
// in os.Stat, which will lead to failure to find & read geoip & geosite files.
|
||||||
|
func TestWrongErrorCheckOnOSStat(t *testing.T) {
|
||||||
|
theExpectedDir := filepath.Join("usr", "local", "share", "v2ray")
|
||||||
|
getAssetLocation := func(file string) string {
|
||||||
|
for _, p := range []string{
|
||||||
|
filepath.Join(theExpectedDir, file),
|
||||||
|
} {
|
||||||
|
// errors.Is(fs.ErrNotExist, err) is a mistake supposed Not to
|
||||||
|
// be discovered by the Go runtime, which will lead to failure to
|
||||||
|
// find & read geoip & geosite files.
|
||||||
|
// The correct code is `errors.Is(err, fs.ErrNotExist)`
|
||||||
|
if _, err := os.Stat(p); err != nil && errors.Is(fs.ErrNotExist, err) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// asset found
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
return filepath.Join("the", "wrong", "path", "not-exist.txt")
|
||||||
|
}
|
||||||
|
|
||||||
|
notExist := getAssetLocation("not-exist.txt")
|
||||||
|
if filepath.Dir(notExist) != theExpectedDir {
|
||||||
|
t.Error("asset dir:", notExist, "not in", theExpectedDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetAssetLocation(t *testing.T) {
|
func TestGetAssetLocation(t *testing.T) {
|
||||||
exec, err := os.Executable()
|
exec, err := os.Executable()
|
||||||
common.Must(err)
|
common.Must(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user