11package router_test
22
33import (
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
2838func TestGeoIPMatcherContainer (t * testing.T ) {
@@ -217,10 +227,15 @@ func TestGeoIPMatcher6US(t *testing.T) {
217227}
218228
219229func 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
0 commit comments