Skip to content
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

Add -trimpath flag #128

Merged
merged 3 commits into from
Sep 5, 2024
Merged

Add -trimpath flag #128

merged 3 commits into from
Sep 5, 2024

Conversation

mfridman
Copy link
Owner

@mfridman mfridman commented Sep 5, 2024

This flag allows the caller to trim the package name which gets displayed in both the package table and tests table.

There's a special case for -trimpath=auto to find the longest common path and use that as the prefix to trim. Almost always this is what a user would want (I think).

Fix #125

Filed a separate issue for the packages that are not being omitted (no tests), #126

Examples:

go test -count=1 ./internal/... -cover -json | tparse

┌──────────────────────────────────────────────────────────────────────────────────────────────┐
│  STATUS │ ELAPSED │                  PACKAGE                  │ COVER  │ PASS │ FAIL │ SKIP  │
│─────────┼─────────┼───────────────────────────────────────────┼────────┼──────┼──────┼───────│
│  PASS   │  0.00s  │ github.com/mfridman/tparse/internal/app   │  0.0%  │  0   │  0   │  0    │
│  PASS   │  0.00s  │ github.com/mfridman/tparse/internal/check │  0.0%  │  0   │  0   │  0    │
│  PASS   │  0.21s  │ github.com/mfridman/tparse/internal/utils │ 100.0% │  11  │  0   │  0    │
└──────────────────────────────────────────────────────────────────────────────────────────────┘
go test -count=1 ./internal/... -cover -json | tparse -trimpath=github.com/mfridman/tparse/

┌───────────────────────────────────────────────────────────────────┐
│  STATUS │ ELAPSED │    PACKAGE     │ COVER  │ PASS │ FAIL │ SKIP  │
│─────────┼─────────┼────────────────┼────────┼──────┼──────┼───────│
│  PASS   │  0.00s  │ internal/app   │  0.0%  │  0   │  0   │  0    │
│  PASS   │  0.00s  │ internal/check │  0.0%  │  0   │  0   │  0    │
│  PASS   │  0.21s  │ internal/utils │ 100.0% │  11  │  0   │  0    │
└───────────────────────────────────────────────────────────────────┘
go test -count=1 ./internal/... -cover -json | tparse -trimpath=github.com/mfridman/tparse/internal

┌────────────────────────────────────────────────────────────┐
│  STATUS │ ELAPSED │ PACKAGE │ COVER  │ PASS │ FAIL │ SKIP  │
│─────────┼─────────┼─────────┼────────┼──────┼──────┼───────│
│  PASS   │  0.00s  │ /app    │  0.0%  │  0   │  0   │  0    │
│  PASS   │  0.00s  │ /check  │  0.0%  │  0   │  0   │  0    │
│  PASS   │  0.21s  │ /utils  │ 100.0% │  11  │  0   │  0    │
└────────────────────────────────────────────────────────────┘
go test -count=1 ./internal/... -cover -json | tparse -trimpath=auto

┌────────────────────────────────────────────────────────────┐
│  STATUS │ ELAPSED │ PACKAGE │ COVER  │ PASS │ FAIL │ SKIP  │
│─────────┼─────────┼─────────┼────────┼──────┼──────┼───────│
│  PASS   │  0.01s  │ app     │  0.0%  │  0   │  0   │  0    │
│  PASS   │  0.01s  │ check   │  0.0%  │  0   │  0   │  0    │
│  PASS   │  0.20s  │ utils   │ 100.0% │  11  │  0   │  0    │
└────────────────────────────────────────────────────────────┘

@@ -0,0 +1,47 @@
package utils
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a few common functions that are fairly important, so I'd like to isolate them into a utils package and add proper tests.

Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor suggestions

@@ -22,6 +22,8 @@ type SummaryTableOptions struct {
// tparse
// /seed-up-down-to-zero
Trim bool

TrimPath string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a comment here, there is one for other variable

for _, pkg := range packages {
names = append(names, pkg.Summary.Package)
}
packagePrefix := utils.FindLongestCommonPrefix(names)

for _, pkg := range packages {
elapsed := strconv.FormatFloat(pkg.Summary.Elapsed, 'f', 2, 64) + "s"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have used

Suggested change
elapsed := strconv.FormatFloat(pkg.Summary.Elapsed, 'f', 2, 64) + "s"
elapsed := pkg.Summary.Elapsed.Truncate(10 * time.Millisecond).String()

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elapsed is a float64, using strconv.FormatFloat seems most appropriate. If it were time.Time then truncate would probably make more sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh indeed, I missed that.

Comment on lines +22 to +25
first, last := paths[0], paths[len(paths)-1]
if first == last {
return first
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the path is something like

foo/bar/foo

Or something like that, my point is if the first last are equal.

I could happen when the project has a replace in the go.mod file to rewrite long project URLs to something shorter

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on what the other paths are. I added a test case just in case, but this is what I'd expect:

{
	paths: []string{
		"foo/bar/foo",
		"foo/foo/foo",
	},
	want: "foo/",
},

@mfridman mfridman merged commit 2f5079b into main Sep 5, 2024
3 checks passed
@mfridman mfridman deleted the mf/gh125 branch September 5, 2024 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add -trimpath flag for long package names
2 participants