Skip to content

Commit

Permalink
feat: support marking pkg as Draft (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed May 4, 2023
1 parent f3bc447 commit 653241a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions gnovm/pkg/gnomod/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

// Parsed gno.mod file.
type File struct {
Draft bool
Module *modfile.Module
Go *modfile.Go
Require []*modfile.Require
Expand Down
4 changes: 4 additions & 0 deletions gnovm/pkg/gnomod/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func Parse(file string, data []byte) (*File, error) {
f.add(&errs, x, l, x.Token[0], l.Token)
}
}
case *modfile.CommentBlock:
if x.Start.Line == 1 {
f.Draft = parseDraft(x)
}
}
}

Expand Down
51 changes: 51 additions & 0 deletions gnovm/pkg/gnomod/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,54 @@ func TestModuleDeprecated(t *testing.T) {
})
}
}

func TestParseDraft(t *testing.T) {
for _, tc := range []struct {
desc, in string
expected bool
}{
{
desc: "no_comment",
in: `module m`,
},
{
desc: "other_comment",
in: `// yo`,
},
{
desc: "draft_no_space",
in: `//Draft`,
expected: true,
},
{
desc: "draft_simple",
in: `// Draft`,
expected: true,
},
{
desc: "draft_lowercase",
in: `// draft`,
},
{
desc: "draft_multiline",
in: `// Draft
// yo`,
},
{
desc: "draft_mixed",
in: `// some other comment
// Draft`,
},
{
desc: "draft_not_first_line",
in: `
// Draft`,
},
} {
t.Run(tc.desc, func(t *testing.T) {
f, err := Parse("in", []byte(tc.in))
assert.Nil(t, err)
assert.Equal(t, tc.expected, f.Draft)
})
}
}
12 changes: 12 additions & 0 deletions gnovm/pkg/gnomod/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,3 +833,15 @@ func parseDirectiveComment(block *modfile.LineBlock, line *modfile.Line) string
}
return strings.Join(lines, "\n")
}

// parseDraft returns whether the module is marked as draft.
func parseDraft(block *modfile.CommentBlock) bool {
if len(block.Before) != 1 {
return false
}
comment := block.Before[0]
if strings.TrimSpace(strings.TrimPrefix(comment.Token, "//")) != "Draft" {
return false
}
return true
}

0 comments on commit 653241a

Please sign in to comment.