-
Notifications
You must be signed in to change notification settings - Fork 24
fix(docs): DSPX-2409 replace SDK README code example with working code #3055
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
marythought
wants to merge
3
commits into
main
Choose a base branch
from
fix/dspx-2409
base: main
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package sdk_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestREADMECodeBlocks verifies that all Go code blocks in the README compile successfully. | ||
| // This ensures the documentation stays accurate and up-to-date with the actual API. | ||
| func TestREADMECodeBlocks(t *testing.T) { | ||
| // Read the README file | ||
| readmePath := filepath.Join("..", "sdk", "README.md") | ||
| content, err := os.ReadFile(readmePath) | ||
| if err != nil { | ||
| t.Fatalf("Failed to read README.md: %v", err) | ||
| } | ||
|
|
||
| // Extract Go code blocks | ||
| codeBlocks := extractGoCodeBlocks(string(content)) | ||
| if len(codeBlocks) == 0 { | ||
| t.Fatal("No Go code blocks found in README.md") | ||
| } | ||
|
|
||
| t.Logf("Found %d Go code block(s) in README.md", len(codeBlocks)) | ||
|
|
||
| // Test each code block that is a complete program | ||
| testedCount := 0 | ||
| for i, code := range codeBlocks { | ||
| // Only test complete programs (those with package main) | ||
| if !strings.Contains(code, "package main") { | ||
| t.Logf("Skipping code block %d (not a complete program)", i+1) | ||
| continue | ||
| } | ||
|
|
||
| testedCount++ | ||
| t.Run(formatBlockName(i, code), func(t *testing.T) { | ||
| if err := testCodeBlock(t, code); err != nil { | ||
| t.Errorf("Code block %d failed to compile:\n%v", i+1, err) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| if testedCount == 0 { | ||
| t.Fatal("No complete program code blocks found in README.md") | ||
| } | ||
| t.Logf("Tested %d complete program(s)", testedCount) | ||
| } | ||
|
|
||
| // extractGoCodeBlocks finds all Go code blocks in the markdown content. | ||
| func extractGoCodeBlocks(content string) []string { | ||
| // Match code blocks that start with ```go and end with ``` | ||
| re := regexp.MustCompile("(?s)```go\n(.*?)```") | ||
| matches := re.FindAllStringSubmatch(content, -1) | ||
|
|
||
| blocks := make([]string, 0, len(matches)) | ||
| for _, match := range matches { | ||
| if len(match) > 1 { | ||
| blocks = append(blocks, match[1]) | ||
| } | ||
| } | ||
| return blocks | ||
| } | ||
|
|
||
| // formatBlockName creates a readable test name from the code block. | ||
| func formatBlockName(index int, code string) string { | ||
| lines := strings.Split(strings.TrimSpace(code), "\n") | ||
| if len(lines) == 0 { | ||
| return "empty_block" | ||
| } | ||
|
|
||
| // Try to find a meaningful identifier in the first few lines | ||
| for _, line := range lines[:min(5, len(lines))] { | ||
| line = strings.TrimSpace(line) | ||
| if strings.HasPrefix(line, "package ") { | ||
| return strings.TrimPrefix(line, "package ") | ||
| } | ||
| if strings.HasPrefix(line, "func ") { | ||
| return strings.Fields(line)[1] | ||
| } | ||
| } | ||
|
|
||
| return "code_block_" + string(rune('A'+index)) | ||
| } | ||
|
|
||
| // testCodeBlock attempts to compile a code block. | ||
| func testCodeBlock(t *testing.T, code string) error { | ||
| // Create a temporary directory | ||
| tmpDir, err := os.MkdirTemp("", "readme-test-*") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Write the code to main.go | ||
| mainPath := filepath.Join(tmpDir, "main.go") | ||
| if err := os.WriteFile(mainPath, []byte(code), 0644); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Initialize go module | ||
| cmd := exec.Command("go", "mod", "init", "example") | ||
| cmd.Dir = tmpDir | ||
| if output, err := cmd.CombinedOutput(); err != nil { | ||
| t.Logf("go mod init output: %s", output) | ||
| return err | ||
| } | ||
|
|
||
| // Get the absolute path to the platform directory | ||
| // When running from sdk directory, we need to go up one level | ||
| platformDir, err := filepath.Abs(filepath.Join("..")) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Add replace directives for local modules | ||
| replacements := []string{ | ||
| "github.com/opentdf/platform/sdk=" + filepath.Join(platformDir, "sdk"), | ||
| "github.com/opentdf/platform/lib/ocrypto=" + filepath.Join(platformDir, "lib/ocrypto"), | ||
| "github.com/opentdf/platform/protocol/go=" + filepath.Join(platformDir, "protocol/go"), | ||
| } | ||
|
|
||
| for _, replace := range replacements { | ||
| cmd := exec.Command("go", "mod", "edit", "-replace", replace) | ||
| cmd.Dir = tmpDir | ||
| if output, err := cmd.CombinedOutput(); err != nil { | ||
| t.Logf("go mod edit output: %s", output) | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Run go mod tidy | ||
| cmd = exec.Command("go", "mod", "tidy") | ||
| cmd.Dir = tmpDir | ||
| if output, err := cmd.CombinedOutput(); err != nil { | ||
| t.Logf("go mod tidy output: %s", output) | ||
| return err | ||
| } | ||
|
|
||
| // Attempt to build | ||
| cmd = exec.Command("go", "build", "-o", "/dev/null", "main.go") | ||
| cmd.Dir = tmpDir | ||
| output, err := cmd.CombinedOutput() | ||
| if err != nil { | ||
| t.Logf("Build output:\n%s", output) | ||
| return err | ||
| } | ||
|
|
||
| t.Logf("Code block compiled successfully") | ||
| return nil | ||
| } | ||
|
|
||
| func min(a, b int) int { | ||
| if a < b { | ||
| return a | ||
| } | ||
| return b | ||
| } |
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.