Skip to content

Commit 7e096bc

Browse files
authored
Chore: Refactor tests in app/router (XTLS#4019)
1 parent 780e89b commit 7e096bc

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

app/router/condition_geoip_test.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package router_test
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
"testing"
@@ -13,16 +14,25 @@ import (
1314
"google.golang.org/protobuf/proto"
1415
)
1516

16-
func init() {
17-
wd, err := os.Getwd()
18-
common.Must(err)
19-
20-
if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
21-
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "resources", "geoip.dat")))
17+
func getAssetPath(file string) (string, error) {
18+
path := platform.GetAssetLocation(file)
19+
_, err := os.Stat(path)
20+
if os.IsNotExist(err) {
21+
path := filepath.Join("..", "..", "resources", file)
22+
_, err := os.Stat(path)
23+
if os.IsNotExist(err) {
24+
return "", fmt.Errorf("can't find %s in standard asset locations or {project_root}/resources", file)
25+
}
26+
if err != nil {
27+
return "", fmt.Errorf("can't stat %s: %v", path, err)
28+
}
29+
return path, nil
2230
}
23-
if _, err := os.Stat(platform.GetAssetLocation("geosite.dat")); err != nil && os.IsNotExist(err) {
24-
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "resources", "geosite.dat")))
31+
if err != nil {
32+
return "", fmt.Errorf("can't stat %s: %v", path, err)
2533
}
34+
35+
return path, nil
2636
}
2737

2838
func TestGeoIPMatcherContainer(t *testing.T) {
@@ -217,10 +227,15 @@ func TestGeoIPMatcher6US(t *testing.T) {
217227
}
218228

219229
func loadGeoIP(country string) ([]*router.CIDR, error) {
220-
geoipBytes, err := filesystem.ReadAsset("geoip.dat")
230+
path, err := getAssetPath("geoip.dat")
221231
if err != nil {
222232
return nil, err
223233
}
234+
geoipBytes, err := filesystem.ReadFile(path)
235+
if err != nil {
236+
return nil, err
237+
}
238+
224239
var geoipList router.GeoIPList
225240
if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
226241
return nil, err

app/router/condition_test.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package router_test
22

33
import (
4-
"os"
5-
"path/filepath"
64
"strconv"
75
"testing"
86

97
. "github.com/xtls/xray-core/app/router"
108
"github.com/xtls/xray-core/common"
119
"github.com/xtls/xray-core/common/errors"
1210
"github.com/xtls/xray-core/common/net"
13-
"github.com/xtls/xray-core/common/platform"
1411
"github.com/xtls/xray-core/common/platform/filesystem"
1512
"github.com/xtls/xray-core/common/protocol"
1613
"github.com/xtls/xray-core/common/protocol/http"
@@ -20,18 +17,6 @@ import (
2017
"google.golang.org/protobuf/proto"
2118
)
2219

23-
func init() {
24-
wd, err := os.Getwd()
25-
common.Must(err)
26-
27-
if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
28-
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
29-
}
30-
if _, err := os.Stat(platform.GetAssetLocation("geosite.dat")); err != nil && os.IsNotExist(err) {
31-
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "release", "config", "geosite.dat")))
32-
}
33-
}
34-
3520
func withBackground() routing.Context {
3621
return &routing_session.Context{}
3722
}
@@ -316,10 +301,15 @@ func TestRoutingRule(t *testing.T) {
316301
}
317302

318303
func loadGeoSite(country string) ([]*Domain, error) {
319-
geositeBytes, err := filesystem.ReadAsset("geosite.dat")
304+
path, err := getAssetPath("geosite.dat")
320305
if err != nil {
321306
return nil, err
322307
}
308+
geositeBytes, err := filesystem.ReadFile(path)
309+
if err != nil {
310+
return nil, err
311+
}
312+
323313
var geositeList GeoSiteList
324314
if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
325315
return nil, err

0 commit comments

Comments
 (0)