Skip to content

Commit

Permalink
fix(test): fixed tests failing
Browse files Browse the repository at this point in the history
  • Loading branch information
ADRFranklin committed Jan 2, 2022
1 parent 3dc9559 commit cb6804b
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 31 deletions.
2 changes: 1 addition & 1 deletion download/extract.go
Expand Up @@ -171,7 +171,7 @@ func Unzip(src, dst string, paths map[string]string) (files map[string]string, e
} else {
targetDir := filepath.Dir(target)
if !util.Exists(targetDir) {
err = os.MkdirAll(targetDir, os.ModePerm)
err = os.MkdirAll(targetDir, 0700)
if err != nil {
return nil, errors.Wrap(err, "failed to create target dir for file")
}
Expand Down
4 changes: 2 additions & 2 deletions pawnpackage/package.go
Expand Up @@ -156,7 +156,7 @@ func (pkg Package) WriteDefinition() (err error) {
if err != nil {
return errors.Wrap(err, "failed to encode package metadata")
}
err = ioutil.WriteFile(filepath.Join(pkg.LocalPath, "pawn.json"), contents, 0755)
err = ioutil.WriteFile(filepath.Join(pkg.LocalPath, "pawn.json"), contents, 0700)
if err != nil {
return errors.Wrap(err, "failed to write pawn.json")
}
Expand All @@ -166,7 +166,7 @@ func (pkg Package) WriteDefinition() (err error) {
if err != nil {
return errors.Wrap(err, "failed to encode package metadata")
}
err = ioutil.WriteFile(filepath.Join(pkg.LocalPath, "pawn.yaml"), contents, 0755)
err = ioutil.WriteFile(filepath.Join(pkg.LocalPath, "pawn.yaml"), contents, 0700)
if err != nil {
return errors.Wrap(err, "failed to write pawn.yaml")
}
Expand Down
4 changes: 2 additions & 2 deletions pawnpackage/package_test.go
Expand Up @@ -121,12 +121,12 @@ func TestPackage_Build(t *testing.T) {
pcxWorkspace := util.FullPath("./tests/build-auto-" + tt.name)
pcxVendor := filepath.Join(pcxWorkspace, "dependencies")

err := os.MkdirAll(filepath.Join(pcxWorkspace, "gamemodes"), 0755)
err := os.MkdirAll(filepath.Join(pcxWorkspace, "gamemodes"), 0700)
if err != nil {
panic(err)
}

err = ioutil.WriteFile(filepath.Join(pcxWorkspace, tt.args.pkg.Entry), tt.sourceCode, 0755)
err = ioutil.WriteFile(filepath.Join(pcxWorkspace, tt.args.pkg.Entry), tt.sourceCode, 0700)
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkgcontext/build.go
Expand Up @@ -88,7 +88,7 @@ func (pcx *PackageContext) Build(
atomic.AddUint32(&buildNumber, 1)

if buildFile != "" {
err2 := ioutil.WriteFile(buildFile, []byte(fmt.Sprint(buildNumber)), 0755)
err2 := ioutil.WriteFile(buildFile, []byte(fmt.Sprint(buildNumber)), 0700)
if err2 != nil {
print.Erro("Failed to write buildfile:", err2)
}
Expand Down Expand Up @@ -246,7 +246,7 @@ loop:
}

if buildFile != "" {
err2 := ioutil.WriteFile(buildFile, []byte(fmt.Sprint(buildNumber)), 0755)
err2 := ioutil.WriteFile(buildFile, []byte(fmt.Sprint(buildNumber)), 0700)
if err2 != nil {
print.Erro("Failed to write buildfile:", err2)
}
Expand Down Expand Up @@ -352,7 +352,7 @@ func readInt(file string) (n uint32, err error) {
}
n = uint32(result)
} else {
err = ioutil.WriteFile(file, []byte("0"), 0755)
err = ioutil.WriteFile(file, []byte("0"), 0700)
n = 0
}
return
Expand Down
2 changes: 1 addition & 1 deletion pkgcontext/cache_test.go
Expand Up @@ -62,7 +62,7 @@ func TestEnsureDependenciesCached(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.RemoveAll(tt.pcx.Package.LocalPath)
os.MkdirAll(tt.pcx.Package.LocalPath, 0755) //nolint
os.MkdirAll(tt.pcx.Package.LocalPath, 0700) //nolint

tt.pcx.GitHub = gh
tt.pcx.GitAuth = gitAuth
Expand Down
2 changes: 1 addition & 1 deletion pkgcontext/ensure_test.go
Expand Up @@ -41,7 +41,7 @@ func TestPackage_EnsureDependencies(t *testing.T) {
}
for _, tt := range tests {
os.RemoveAll(tt.pcx.Package.LocalPath)
os.MkdirAll(tt.pcx.Package.LocalPath, 0755) //nolint
os.MkdirAll(tt.pcx.Package.LocalPath, 0700) //nolint

tt.pcx.GitHub = gh
tt.pcx.Platform = runtime.GOOS
Expand Down
4 changes: 2 additions & 2 deletions rook/install_test.go
Expand Up @@ -44,9 +44,9 @@ func Test_PackageInstall(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
dir := filepath.Join("./tests/install", tt.name)
os.RemoveAll(dir)
os.MkdirAll(dir, 0755)
os.MkdirAll(dir, 0700)

ioutil.WriteFile(filepath.Join(dir, "pawn.json"), tt.pkg, 0755) // nolint
ioutil.WriteFile(filepath.Join(dir, "pawn.json"), tt.pkg, 0700) // nolint

pcx1, err := pkgcontext.NewPackageContext(gh, gitAuth, true, dir, runtime.GOOS, "./tests/cache", "", false)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions runtime/all_test.go
Expand Up @@ -46,9 +46,9 @@ func TestMain(m *testing.M) {
}

func fakeServerDir(path string) {
os.MkdirAll(filepath.Join(path, "gamemodes"), 0755)
os.MkdirAll(filepath.Join(path, "filterscripts"), 0755)
os.MkdirAll(filepath.Join(path, "plugins"), 0755)
os.MkdirAll(filepath.Join(path, "gamemodes"), 0700)
os.MkdirAll(filepath.Join(path, "filterscripts"), 0700)
os.MkdirAll(filepath.Join(path, "plugins"), 0700)
f, _ := os.Create(filepath.Join(path, "gamemodes", "rivershell.amx"))
f.Close() // nolint
f, _ = os.Create(filepath.Join(path, "filterscripts", "admin.amx"))
Expand Down
47 changes: 33 additions & 14 deletions runtime/ensure_plugins.go
Expand Up @@ -155,7 +155,15 @@ func EnsureVersionedPlugin(
} else {
print.Verb(meta, "plugin resource is a single file")
base := filepath.Base(filename)
destination := filepath.Join(dir, "plugins", base)
finalDir := filepath.Join(dir, "plugins")
destination := filepath.Join(finalDir, base)

err = os.MkdirAll(finalDir, 0700)
if err != nil {
err = errors.Wrapf(err, "failed to create path for plugin resource %s to %s", filename, destination)
return
}

err = util.CopyFile(filename, destination)
if err != nil {
err = errors.Wrapf(err, "failed to copy non-archive file %s to %s", filename, destination)
Expand Down Expand Up @@ -214,7 +222,6 @@ func PluginFromCache(
cacheDir string,
) (hit bool, filename string, resource *resource.Resource, err error) {
resourcePath := filepath.Join(cacheDir, GetResourcePath(meta))

print.Verb("getting plugin resource from cache", meta, resourcePath)

pkg, err := pawnpackage.GetCachedPackage(meta, cacheDir)
Expand All @@ -224,6 +231,9 @@ func PluginFromCache(
hit = false
return
}
if pkg.Format == "" {
return
}

files, err := ioutil.ReadDir(resourcePath)
if err != nil {
Expand Down Expand Up @@ -275,22 +285,19 @@ func PluginFromNet(
print.Info(meta, "downloading plugin resource for", platform)

resourcePathOnly := GetResourcePath(meta)
resourcePath := filepath.Join(cacheDir, resourcePathOnly)
resourcePath := filepath.Join(cacheDir, resourcePathOnly)

err = os.MkdirAll(resourcePath, 0700)
if err != nil {
err = errors.Wrap(err, "failed to create cache directory for package resources")
return
}

pkg, err := pawnpackage.GetCachedPackage(meta, cacheDir)
pkg, err := pawnpackage.GetRemotePackage(ctx, gh, meta)
if err != nil {
pkg, err = pawnpackage.GetRemotePackage(ctx, gh, meta)
if err != nil {
err = errors.Wrap(err, "failed to get remote package definition file")
return
}
}
err = errors.Wrap(err, "failed to get remote package definition file")
return
}

resource, err = GetResource(pkg.Resources, platform, version)
if err != nil {
Expand All @@ -317,20 +324,32 @@ func PluginFromNet(
func GetResource(resources []resource.Resource, platform string, version string) (*resource.Resource, error) {
if version == "" {
version = "0.3.7"
}
}

found := false
var tmp *resource.Resource
for _, res := range resources {
if res.Platform == platform {
if res.Version == "" || res.Version == version {
if res.Version == version {
tmp = &res
found = true
break
}
}
}
if tmp == nil {
return nil, errors.Errorf("plugin does not provide binaries for target platform %s and version %s", platform, version)
if !found {
for _, res := range resources {
if res.Platform == platform && res.Version == "" {
print.Verb("no resource matching version: ", version, ", falling back to the first resource matching platform: ", platform)
tmp = &res
found = true
break;
}
}
}
if !found {
return nil, errors.Errorf("plugin does not provide binaries for target platform %s and/or version %s", platform, version)
}

if err := tmp.Validate(); err != nil {
return nil, errors.Wrap(err, "matching resource found but is invalid")
Expand Down
2 changes: 1 addition & 1 deletion runtime/ensure_plugins_test.go
Expand Up @@ -70,7 +70,7 @@ func TestEnsurePlugins(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.cfg.WorkingDir = filepath.Join("./tests/ensure", tt.name)
os.MkdirAll(tt.args.cfg.WorkingDir, 0755)
os.MkdirAll(tt.args.cfg.WorkingDir, 0700)

t.Log("First call to Ensure - from internet")
err := EnsurePlugins(context.Background(), gh, &tt.args.cfg, "./tests/cache", true)
Expand Down
2 changes: 1 addition & 1 deletion util/util_test.go
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestMain(m *testing.M) {
os.MkdirAll("./tests", 0755)
os.MkdirAll("./tests", 0700)

f, _ := os.Create("./tests/file")
f.Close() // nolint
Expand Down

0 comments on commit cb6804b

Please sign in to comment.