-
Notifications
You must be signed in to change notification settings - Fork 347
Added _test package support #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IlyasYOY
wants to merge
3
commits into
cweill:develop
Choose a base branch
from
IlyasYOY:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| package gomod_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
|
|
||
| "github.com/cweill/gotests/internal/gomod" | ||
| ) | ||
|
|
||
| func ExampleGetFullImportPath_file() { | ||
| // Get import path for a specific Go file | ||
| importPath, err := gomod.GetFullImportPath("gomod.go") | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| fmt.Println(importPath) | ||
| // Output: github.com/cweill/gotests/internal/gomod | ||
| } | ||
|
|
||
| func ExampleGetFullImportPath_directory() { | ||
| // Get import path for a package directory | ||
| importPath, err := gomod.GetFullImportPath(".") | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| fmt.Println(importPath) | ||
| // Output: github.com/cweill/gotests/internal/gomod | ||
| } | ||
|
|
||
| func ExampleGetFullImportPath_moduleRoot() { | ||
| // Get import path for the module root directory | ||
| importPath, err := gomod.GetFullImportPath("../..") | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| fmt.Println(importPath) | ||
| // Output: github.com/cweill/gotests | ||
| } |
This file contains hidden or 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,106 @@ | ||
| package gomod | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // GetFullImportPath resolves the full Go import path for any file or directory | ||
| // within a Go module. Returns the complete import path like "github.com/user/repo/pkg". | ||
| // | ||
| // startAt can be either: | ||
| // - A Go source file path (e.g., "/path/to/project/main.go") | ||
| // - A directory path (e.g., "/path/to/project/pkg") | ||
| // - An absolute or relative path | ||
| // | ||
| // Returns an error if: | ||
| // - No go.mod found in the directory tree | ||
| // - go.mod is malformed or missing module directive | ||
| // - Path resolution fails | ||
| func GetFullImportPath(startAt string) (string, error) { | ||
| absPath, err := filepath.Abs(startAt) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to get absolute path for %s: %w", startAt, err) | ||
| } | ||
|
|
||
| // If it's a file, get its directory | ||
| if info, err := os.Stat(absPath); err == nil && !info.IsDir() { | ||
| absPath = filepath.Dir(absPath) | ||
| } | ||
|
|
||
| modRoot, err := findGoMod(absPath) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| modulePath, err := parseModulePath(modRoot) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| relPath, err := filepath.Rel(modRoot, absPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to calculate relative path from %s to %s: %w", modRoot, absPath, err) | ||
| } | ||
|
|
||
| if relPath == "." { | ||
| return modulePath, nil | ||
| } | ||
|
|
||
| return filepath.Join(modulePath, relPath), nil | ||
| } | ||
|
|
||
| // findGoMod walks up the directory tree from startDir to find a go.mod file. | ||
| // Returns the directory containing go.mod, or an error if not found. | ||
| func findGoMod(startDir string) (string, error) { | ||
| current := startDir | ||
|
|
||
| for { | ||
| modPath := filepath.Join(current, "go.mod") | ||
| if _, err := os.Stat(modPath); err == nil { | ||
| return current, nil | ||
| } | ||
|
|
||
| parent := filepath.Dir(current) | ||
| if parent == current { | ||
| // Reached root directory | ||
| break | ||
| } | ||
| current = parent | ||
| } | ||
|
|
||
| return "", fmt.Errorf("go.mod not found in %s or any parent directory", startDir) | ||
| } | ||
|
|
||
| // parseModulePath reads the go.mod file in modRoot and extracts the module path. | ||
| // Returns the module path or an error if parsing fails. | ||
| func parseModulePath(modRoot string) (string, error) { | ||
| modFile := filepath.Join(modRoot, "go.mod") | ||
|
|
||
| file, err := os.Open(modFile) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to open go.mod at %s: %w", modFile, err) | ||
| } | ||
| defer file.Close() | ||
|
|
||
| scanner := bufio.NewScanner(file) | ||
| for scanner.Scan() { | ||
| line := strings.TrimSpace(scanner.Text()) | ||
| if strings.HasPrefix(line, "module ") { | ||
| modulePath := strings.TrimSpace(line[7:]) // Remove "module " prefix | ||
| if modulePath == "" { | ||
| return "", fmt.Errorf("empty module path in %s", modFile) | ||
| } | ||
| return modulePath, nil | ||
| } | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return "", fmt.Errorf("error reading go.mod at %s: %w", modFile, err) | ||
| } | ||
|
|
||
| return "", fmt.Errorf("module directive not found in %s", modFile) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must mention that the
gomodwas generated by AI and reviewed by myself.Everything else was done manually.