-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
global: make gremlins work on packages
- Loading branch information
Showing
16 changed files
with
318 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2022 The Gremlins Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package gomodule | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// GoModule represents the current execution context in Gremlins. | ||
// | ||
// Name is the module name of the Go module being tested by Gremlins. | ||
// Root is the root folder of the Go module. | ||
// PkgDir is the folder in which Gremlins is running. | ||
type GoModule struct { | ||
Name string | ||
Root string | ||
PkgDir string | ||
} | ||
|
||
// Init initializes the current module. It finds the module name and the root | ||
// of the module, then returns a GoModule struct. | ||
func Init(path string) (GoModule, error) { | ||
if path == "" { | ||
return GoModule{}, fmt.Errorf("path is not set") | ||
} | ||
mod, root, err := getMod(path) | ||
if err != nil { | ||
return GoModule{}, err | ||
} | ||
path, _ = filepath.Rel(root, path) | ||
|
||
return GoModule{ | ||
Name: mod, | ||
Root: root, | ||
PkgDir: path, | ||
}, nil | ||
} | ||
|
||
func getMod(path string) (string, string, error) { | ||
root := findModuleRoot(path) | ||
file, err := os.Open(root + "/go.mod") | ||
defer func(file *os.File) { | ||
_ = file.Close() | ||
}(file) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
r := bufio.NewReader(file) | ||
line, _, err := r.ReadLine() | ||
if err != nil { | ||
return "", "", err | ||
} | ||
packageName := bytes.TrimPrefix(line, []byte("module ")) | ||
|
||
return string(packageName), root, nil | ||
} | ||
|
||
func findModuleRoot(path string) string { | ||
// Inspired by how Go itself finds the module root. | ||
path = filepath.Clean(path) | ||
for { | ||
if fi, err := os.Stat(filepath.Join(path, "go.mod")); err == nil && !fi.IsDir() { | ||
return path | ||
} | ||
d := filepath.Dir(path) | ||
if d == path { | ||
break | ||
} | ||
path = d | ||
} | ||
|
||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2022 The Gremlins Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package gomodule_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/go-gremlins/gremlins/internal/gomodule" | ||
) | ||
|
||
func TestDetectsModule(t *testing.T) { | ||
t.Run("does not return error if it can retrieve module", func(t *testing.T) { | ||
const modName = "example.com" | ||
rootDir := t.TempDir() | ||
pkgDir := "pkgDir" | ||
absPkgDir := filepath.Join(rootDir, pkgDir) | ||
_ = os.MkdirAll(absPkgDir, 0600) | ||
goMod := filepath.Join(rootDir, "go.mod") | ||
err := os.WriteFile(goMod, []byte("module "+modName), 0600) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
mod, err := gomodule.Init(absPkgDir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if mod.Name != modName { | ||
t.Errorf("expected Go module to be %q, got %q", modName, mod.Name) | ||
} | ||
if mod.Root != rootDir { | ||
t.Errorf("expected Go root to be %q, got %q", rootDir, mod.Root) | ||
} | ||
if mod.PkgDir != pkgDir { | ||
t.Errorf("expected Go package dir to be %q, got %q", pkgDir, mod.PkgDir) | ||
} | ||
}) | ||
|
||
t.Run("returns error if go.mod is invalid", func(t *testing.T) { | ||
path := t.TempDir() | ||
goMod := path + "/go.mod" | ||
err := os.WriteFile(goMod, []byte(""), 0600) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, err = gomodule.Init(path) | ||
if err == nil { | ||
t.Errorf("expected an error") | ||
} | ||
}) | ||
|
||
t.Run("returns error if it cannot find module", func(t *testing.T) { | ||
_, err := gomodule.Init(t.TempDir()) | ||
if err == nil { | ||
t.Errorf("expected an error") | ||
} | ||
}) | ||
|
||
t.Run("returns error if path is empty", func(t *testing.T) { | ||
_, err := gomodule.Init("") | ||
if err == nil { | ||
t.Errorf("expected an error") | ||
} | ||
}) | ||
} |
Oops, something went wrong.