-
Notifications
You must be signed in to change notification settings - Fork 375
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
feat(gnovm): Executable Markdown #2357
Open
notJoon
wants to merge
47
commits into
gnolang:master
Choose a base branch
from
notJoon:testable-markdown
base: master
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 29 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
1e598f6
fenced code block parser
notJoon 168259d
file handler
notJoon edb648e
pseudo code for execute a parsed code
notJoon f60cb1c
generate hash key based on the parsed code
notJoon a577afc
use tree-sitter for parsing markdown content
notJoon b29530c
capturing output from buffer
notJoon 6430862
refactor
notJoon b7ef8ad
save
notJoon b649727
change to use MemDB and add execute options
notJoon e53482b
save: need to handle import expr later
notJoon f0df3b3
basic CLI
notJoon 599bd6f
execute imported function
notJoon da567e3
save
notJoon 2d363bc
fix: doctest CLI
notJoon 83d7ca0
Merge branch 'master' into testable-markdown
notJoon f31a102
lint
notJoon 4f05a8e
Merge branch 'testable-markdown' of https://github.com/notJoon/gno-co…
notJoon 23052b9
static analysis for auto fix missing package and import statement
notJoon fc94da3
Merge branch 'master' into testable-markdown
notJoon 3844365
remove tree-sitter to avoid cgo linter problem
notJoon c8b0aef
reorganize
notJoon 5a8a2d8
add test
notJoon 86bdb99
wip: pkgloader
notJoon fbe7ddf
fix difference behavior with local and test
notJoon e001a33
Merge branch 'master' into testable-markdown
notJoon e648334
fix lint
notJoon 82e02f0
expect result
notJoon 273abb5
auto test name generator
notJoon 1ee5486
use pattern when run code block instead index
notJoon 4775fd3
generate better name
notJoon 855bbc5
add execution option and support regex match for expected output
notJoon cbc060f
Merge branch 'master' into testable-markdown
notJoon ad329eb
ignore non-go language, add timeout
notJoon bf480c7
Merge branch 'master' into testable-markdown
notJoon 1f7f238
Update gnovm/pkg/doctest/analyzer.go
notJoon 1bb104a
Apply suggestions from code review
notJoon 6bac3ad
resolve suggestions (doctest, exec, parser)
notJoon 4978329
fix: partial exec, parser
notJoon 1577034
update: compareResults, ExecuteBlock
notJoon 2d64669
remove unnecessary lock
notJoon 317fd41
fix: handle auto import for conflict names
notJoon ae25209
readme
notJoon 45265f2
remove auto import
notJoon b7a3d9d
Merge branch 'master' into testable-markdown
notJoon 1776969
Merge branch 'master' into testable-markdown
notJoon 3142978
fix execution errors
notJoon d15d3f1
fix
notJoon 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 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,84 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
dt "github.com/gnolang/gno/gnovm/pkg/doctest" | ||
"github.com/gnolang/gno/tm2/pkg/commands" | ||
) | ||
|
||
type doctestCfg struct { | ||
markdownPath string | ||
// codeIndex int | ||
runPattern string | ||
} | ||
|
||
func newDoctestCmd(io commands.IO) *commands.Command { | ||
cfg := &doctestCfg{} | ||
|
||
return commands.NewCommand( | ||
commands.Metadata{ | ||
Name: "doctest", | ||
ShortUsage: "doctest -path <markdown_file_path> [-run <pattern>]", | ||
ShortHelp: "executes a specific code block from a markdown file", | ||
}, | ||
cfg, | ||
func(_ context.Context, args []string) error { | ||
return execDoctest(cfg, args, io) | ||
}, | ||
) | ||
} | ||
|
||
func (c *doctestCfg) RegisterFlags(fs *flag.FlagSet) { | ||
fs.StringVar( | ||
&c.markdownPath, | ||
"path", | ||
"", | ||
"path to the markdown file", | ||
) | ||
fs.StringVar( | ||
&c.runPattern, | ||
"run", | ||
"", | ||
"pattern to match code block names", | ||
) | ||
} | ||
|
||
func execDoctest(cfg *doctestCfg, _ []string, io commands.IO) error { | ||
if cfg.markdownPath == "" { | ||
return fmt.Errorf("markdown file path is required") | ||
} | ||
|
||
content, err := fetchMarkdown(cfg.markdownPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to read markdown file: %w", err) | ||
} | ||
|
||
results, err := dt.ExecuteMatchingCodeBlock(content, cfg.runPattern) | ||
if err != nil { | ||
return fmt.Errorf("failed to execute code block: %w", err) | ||
} | ||
|
||
if len(results) == 0 { | ||
io.Println("No code blocks matched the pattern") | ||
return nil | ||
} | ||
|
||
io.Println("Execution Result:") | ||
io.Println(strings.Join(results, "\n\n")) | ||
|
||
return nil | ||
} | ||
|
||
// fetchMarkdown reads a markdown file and returns its content | ||
func fetchMarkdown(path string) (string, error) { | ||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read file: %w", err) | ||
} | ||
return string(content), nil | ||
} |
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,105 @@ | ||
package main | ||
|
||
// import ( | ||
// "os" | ||
// "testing" | ||
// ) | ||
|
||
// func TestDoctest(t *testing.T) { | ||
// tempDir, err := os.MkdirTemp("", "doctest-test") | ||
// if err != nil { | ||
// t.Fatalf("failed to create temp directory: %v", err) | ||
// } | ||
// defer os.RemoveAll(tempDir) | ||
|
||
// markdownContent := `# Go Code Examples | ||
|
||
// This document contains two simple examples written in Go. | ||
|
||
// ## Example 1: Fibonacci Sequence | ||
|
||
// The first example prints the first 10 numbers of the Fibonacci sequence. | ||
|
||
// ` + "```go" + ` | ||
// // @test: Fibonacci | ||
// package main | ||
|
||
// func main() { | ||
// a, b := 0, 1 | ||
// for i := 0; i < 10; i++ { | ||
// println(a) | ||
// a, b = b, a+b | ||
// } | ||
// } | ||
// ` + "```" + ` | ||
|
||
// ## Example 2: String Reversal | ||
|
||
// The second example reverses a given string and prints it. | ||
|
||
// ` + "```go" + ` | ||
// // @test: StringReversal | ||
// package main | ||
|
||
// func main() { | ||
// str := "Hello, Go!" | ||
// runes := []rune(str) | ||
// for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { | ||
// runes[i], runes[j] = runes[j], runes[i] | ||
// } | ||
// println(string(runes)) | ||
// } | ||
// ` + "```" + ` | ||
|
||
// These two examples demonstrate basic Go functionality without using concurrency, generics, or reflect. | ||
|
||
// ` + "## std Package" + ` | ||
// ` + "```go" + ` | ||
// // @test: StdPackage | ||
// package main | ||
|
||
// import ( | ||
// "std" | ||
// ) | ||
|
||
// func main() { | ||
// addr := std.GetOrigCaller() | ||
// println(addr) | ||
// } | ||
// ` + "```" + ` | ||
// ` | ||
|
||
// mdFile, err := os.CreateTemp(tempDir, "sample-*.md") | ||
// if err != nil { | ||
// t.Fatalf("failed to create temp file: %v", err) | ||
// } | ||
// defer mdFile.Close() | ||
|
||
// _, err = mdFile.WriteString(markdownContent) | ||
// if err != nil { | ||
// t.Fatalf("failed to write to temp file: %v", err) | ||
// } | ||
|
||
// mdFilePath := mdFile.Name() | ||
|
||
// tc := []testMainCase{ | ||
// { | ||
// args: []string{"doctest", "-h"}, | ||
// errShouldBe: "flag: help requested", | ||
// }, | ||
// { | ||
// args: []string{"doctest", "-path", mdFilePath, "-run", "Fibonacci"}, | ||
// stdoutShouldContain: "--- Fibonacci ---\n0\n1\n1\n2\n3\n5\n8\n13\n21\n34\n", | ||
// }, | ||
// { | ||
// args: []string{"doctest", "-path", mdFilePath, "-run", "StringReversal"}, | ||
// stdoutShouldContain: "--- StringReversal ---\n!oG ,olleH\n", | ||
// }, | ||
// { | ||
// args: []string{"doctest", "-path", mdFilePath, "-run", "StdPackage"}, | ||
// stdoutShouldContain: "--- StdPackage ---\ng14ch5q26mhx3jk5cxl88t278nper264ces4m8nt\n", | ||
// }, | ||
// } | ||
|
||
// testMainCaseRun(t, tc) | ||
// } |
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,87 @@ | ||
# Gno Doctest: Easy Code Execution and Testing | ||
|
||
Gno Doctest is a tool that allows you to easily execute and test code blocks written in the Gno language. This tool offers a range of features, from simple code execution to complex package imports. | ||
|
||
## 1. Basic Code Execution | ||
|
||
Even the simplest form of code block can be easily executed. | ||
|
||
```go | ||
package main | ||
|
||
func main() { | ||
println("Hello, World!") | ||
} | ||
``` | ||
|
||
Doctest also recognizes that a block of code is a gno. The code below outputs the same result as the example above. | ||
|
||
```go | ||
package main | ||
|
||
func main() { | ||
println("Hello, World!") | ||
} | ||
``` | ||
|
||
Running this code will output "Hello, World!". | ||
|
||
## 2. Using Standard Library Packages | ||
|
||
Gno Doctest automatically recognizes and imports standard library packages. | ||
|
||
```go | ||
package main | ||
|
||
import "std" | ||
|
||
func main() { | ||
addr := std.GetOrigCaller() | ||
println(addr) | ||
} | ||
``` | ||
|
||
## 3. Utilizing Various Standard Libraries | ||
|
||
You can use multiple standard libraries simultaneously. | ||
|
||
```gno | ||
package main | ||
|
||
import "strings" | ||
|
||
func main() { | ||
println(strings.ToUpper("Hello, World")) | ||
} | ||
``` | ||
|
||
This example uses the ToUpper() function from the strings package to convert a string to uppercase. | ||
|
||
## 4. Automatic Package Import | ||
|
||
One of the most powerful features of Gno Doctest is its ability to handle package declarations and imports automatically. | ||
|
||
```go | ||
func main() { | ||
println(math.Pi) | ||
println(strings.ToUpper("Hello, World")) | ||
} | ||
``` | ||
|
||
In this code, the math and strings packages are not explicitly imported, but Doctest automatically recognizes and imports the necessary packages. | ||
|
||
## 5. Omitting Package Declaration | ||
|
||
Doctest can even handle cases where the package main declaration is omitted. | ||
|
||
```go | ||
func main() { | ||
s := strings.ToUpper("Hello, World") | ||
println(s) | ||
} | ||
``` | ||
|
||
This code runs normally without package declaration or import statements. | ||
Using Gno Doctest makes code execution and testing much more convenient. | ||
|
||
You can quickly run various Gno code snippets and check the results without complex setups. |
Oops, something went wrong.
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.
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 don't get where you're putting the output?
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.
The execution results are not separately saved (although they are stored in the cache) and are only shown through the CLI. At first, I thought this would be sufficient, but it seems that saving the values would be more useful.