Skip to content

Commit

Permalink
fix: allow LICENSE rendering via gnoweb (#2417)
Browse files Browse the repository at this point in the history
We allow LICENSE files to be included when adding packages, so we should
also allow them to be viewed via gnoweb.

<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [x] Provided any useful hints for running manual tests
- [x] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
  • Loading branch information
deelawn authored Jun 24, 2024
1 parent 900bb2f commit bedd0f9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/gnoweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func MakeApp(logger *slog.Logger, cfg Config) gotuna.App {
}
// realm routes
// NOTE: see rePathPart.
app.Router.Handle("/r/{rlmname:[a-z][a-z0-9_]*(?:/[a-z][a-z0-9_]*)+}/{filename:(?:.*\\.(?:gno|md|txt|mod)$)?}", handlerRealmFile(logger, app, &cfg))
app.Router.Handle("/r/{rlmname:[a-z][a-z0-9_]*(?:/[a-z][a-z0-9_]*)+}/{filename:(?:(?:.*\\.(?:gno|md|txt|mod)$)|(?:LICENSE$))?}", handlerRealmFile(logger, app, &cfg))
app.Router.Handle("/r/{rlmname:[a-z][a-z0-9_]*(?:/[a-z][a-z0-9_]*)+}", handlerRealmMain(logger, app, &cfg))
app.Router.Handle("/r/{rlmname:[a-z][a-z0-9_]*(?:/[a-z][a-z0-9_]*)+}:{querystr:.*}", handlerRealmRender(logger, app, &cfg))
app.Router.Handle("/p/{filepath:.*}", handlerPackageFile(logger, app, &cfg))
Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/gnoweb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestRoutes(t *testing.T) {
{"/%ED%85%8C%EC%8A%A4%ED%8A%B8", notFound, "/테스트"},
{"/グノー", notFound, "/グノー"},
{"/⚛️", notFound, "/⚛️"},
{"/p/demo/flow/LICENSE", ok, "BSD 3-Clause"},
}

config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
Expand All @@ -67,7 +68,6 @@ func TestRoutes(t *testing.T) {
response := httptest.NewRecorder()
app.Router.ServeHTTP(response, request)
assert.Equal(t, r.status, response.Code)

assert.Contains(t, response.Body.String(), r.substring)
})
}
Expand Down
15 changes: 10 additions & 5 deletions tm2/pkg/std/memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,23 @@ func (mempkg *MemPackage) Validate() error {
return nil
}

const licenseName = "LICENSE"

// Splits a path into the dirpath and filename.
func SplitFilepath(filepath string) (dirpath string, filename string) {
parts := strings.Split(filepath, "/")
if len(parts) == 1 {
return parts[0], ""
}
last := parts[len(parts)-1]
if strings.Contains(last, ".") {

switch last := parts[len(parts)-1]; {
case strings.Contains(last, "."):
return strings.Join(parts[:len(parts)-1], "/"), last
} else if last == "" {
case last == "":
return strings.Join(parts[:len(parts)-1], "/"), ""
} else {
return strings.Join(parts, "/"), ""
case last == licenseName:
return strings.Join(parts[:len(parts)-1], "/"), licenseName
}

return strings.Join(parts, "/"), ""
}
51 changes: 51 additions & 0 deletions tm2/pkg/std/memfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,54 @@ func TestMemPackage_Validate(t *testing.T) {
})
}
}

func TestSplitFilepath(t *testing.T) {
t.Parallel()
tests := []struct {
name string
filepath string
expDirPath string
expFilename string
}{
{
name: "empty",
},
{
name: "one part",
filepath: "root",
expDirPath: "root",
},
{
name: "file",
filepath: "gno.land/r/demo/avl/avl.gno",
expDirPath: "gno.land/r/demo/avl",
expFilename: "avl.gno",
},
{
name: "trailing slash",
filepath: "gno.land/r/demo/avl/",
expDirPath: "gno.land/r/demo/avl",
},
{
name: "license",
filepath: "gno.land/r/demo/avl/LICENSE",
expDirPath: "gno.land/r/demo/avl",
expFilename: "LICENSE",
},
{
name: "regular path",
filepath: "gno.land/p/demo/avl",
expDirPath: "gno.land/p/demo/avl",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
dirPath, filename := SplitFilepath(tt.filepath)
assert.Equal(t, tt.expDirPath, dirPath)
assert.Equal(t, tt.expFilename, filename)
})
}
}

0 comments on commit bedd0f9

Please sign in to comment.