Skip to content

Commit

Permalink
Chore: Refactor tests in app/router (#4019)
Browse files Browse the repository at this point in the history
  • Loading branch information
zonescape authored Nov 25, 2024
1 parent 384d079 commit 034a485
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
33 changes: 24 additions & 9 deletions app/router/condition_geoip_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package router_test

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -13,16 +14,25 @@ import (
"google.golang.org/protobuf/proto"
)

func init() {
wd, err := os.Getwd()
common.Must(err)

if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "resources", "geoip.dat")))
func getAssetPath(file string) (string, error) {
path := platform.GetAssetLocation(file)
_, err := os.Stat(path)
if os.IsNotExist(err) {
path := filepath.Join("..", "..", "resources", file)
_, err := os.Stat(path)
if os.IsNotExist(err) {
return "", fmt.Errorf("can't find %s in standard asset locations or {project_root}/resources", file)
}
if err != nil {
return "", fmt.Errorf("can't stat %s: %v", path, err)
}
return path, nil
}
if _, err := os.Stat(platform.GetAssetLocation("geosite.dat")); err != nil && os.IsNotExist(err) {
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "resources", "geosite.dat")))
if err != nil {
return "", fmt.Errorf("can't stat %s: %v", path, err)
}

return path, nil
}

func TestGeoIPMatcherContainer(t *testing.T) {
Expand Down Expand Up @@ -217,10 +227,15 @@ func TestGeoIPMatcher6US(t *testing.T) {
}

func loadGeoIP(country string) ([]*router.CIDR, error) {
geoipBytes, err := filesystem.ReadAsset("geoip.dat")
path, err := getAssetPath("geoip.dat")
if err != nil {
return nil, err
}
geoipBytes, err := filesystem.ReadFile(path)
if err != nil {
return nil, err
}

var geoipList router.GeoIPList
if err := proto.Unmarshal(geoipBytes, &geoipList); err != nil {
return nil, err
Expand Down
22 changes: 6 additions & 16 deletions app/router/condition_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package router_test

import (
"os"
"path/filepath"
"strconv"
"testing"

. "github.com/xtls/xray-core/app/router"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/common/platform/filesystem"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/protocol/http"
Expand All @@ -20,18 +17,6 @@ import (
"google.golang.org/protobuf/proto"
)

func init() {
wd, err := os.Getwd()
common.Must(err)

if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
}
if _, err := os.Stat(platform.GetAssetLocation("geosite.dat")); err != nil && os.IsNotExist(err) {
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "release", "config", "geosite.dat")))
}
}

func withBackground() routing.Context {
return &routing_session.Context{}
}
Expand Down Expand Up @@ -316,10 +301,15 @@ func TestRoutingRule(t *testing.T) {
}

func loadGeoSite(country string) ([]*Domain, error) {
geositeBytes, err := filesystem.ReadAsset("geosite.dat")
path, err := getAssetPath("geosite.dat")
if err != nil {
return nil, err
}
geositeBytes, err := filesystem.ReadFile(path)
if err != nil {
return nil, err
}

var geositeList GeoSiteList
if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
return nil, err
Expand Down

0 comments on commit 034a485

Please sign in to comment.