Skip to content

Commit

Permalink
passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pastuxso committed Nov 2, 2023
1 parent a25978d commit be85975
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 63 deletions.
2 changes: 1 addition & 1 deletion internal/document/editor/cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Test paragraph
testDataFrontmatterJSON = []byte(strings.TrimSpace(`
---
{
shell: fish
"shell": "fish"
}
---
Expand Down
2 changes: 1 addition & 1 deletion internal/document/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Deserialize(data []byte) (*Notebook, error) {
fmtr, info := document.ParseFrontmatter(string(sections.FrontMatter))
fmtr.EnsureID()

frontmatterStr := string(document.StringifyFrontmatter(fmtr, info))
frontmatterStr := document.StringifyFrontmatter(string(sections.FrontMatter), fmtr, info)

if frontmatterStr != "" {
notebook.Metadata[PrefixAttributeName(InternalAttributePrefix, FrontmatterKey)] = frontmatterStr
Expand Down
18 changes: 14 additions & 4 deletions internal/document/editor/editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package editor
import (
"bytes"
"encoding/json"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/stateful/runme/internal/document/constants"
"github.com/stateful/runme/internal/idgen"
"github.com/stateful/runme/internal/version"
)

func TestEditor(t *testing.T) {
Expand Down Expand Up @@ -145,15 +148,22 @@ Paragraph 1 with a link [Link1](https://example.com 'Link Title 1') and a second
}

func TestEditor_FrontMatter(t *testing.T) {
data := []byte(`+++
prop1 = "val1"
prop2 = "val2"
id := idgen.GenerateID()
idgen.MockGenerator(id)

data := []byte(fmt.Sprintf(`+++
prop1 = 'val1'
prop2 = 'val2'
[runme]
id = '%s'
version = '%s'
+++
# Example
A paragraph
`)
`, id, version.BuildVersion))
notebook, err := Deserialize(data)
require.NoError(t, err)
result, err := Serialize(notebook)
Expand Down
14 changes: 12 additions & 2 deletions internal/document/editor/editorservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package editorservice

import (
"context"
"fmt"
"net"
"os"
"testing"

"github.com/stateful/runme/internal/document/editor"
parserv1 "github.com/stateful/runme/internal/gen/proto/go/runme/parser/v1"
"github.com/stateful/runme/internal/idgen"
"github.com/stateful/runme/internal/version"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
Expand All @@ -18,6 +21,10 @@ import (
)

func Test_parserServiceServer(t *testing.T) {
id := idgen.GenerateID()
idgen.MockGenerator(id)
defer idgen.ResetGenerator()

lis := bufconn.Listen(2048)
server := grpc.NewServer()
parserv1.RegisterParserServiceServer(server, NewParserServiceServer(zap.NewNop()))
Expand Down Expand Up @@ -76,9 +83,12 @@ func Test_parserServiceServer(t *testing.T) {
})

t.Run("Frontmatter", func(t *testing.T) {
frontMatter := `---
frontMatter := fmt.Sprintf(`---
prop: value
---`
runme:
id: %s
version: %s
---`, id, version.BuildVersion)
content := `# Hello
Some content
Expand Down
112 changes: 60 additions & 52 deletions internal/document/frontmatter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package document

import (
"bytes"
byteslib "bytes"
"encoding/json"
"errors"
"fmt"
Expand All @@ -10,14 +10,13 @@ import (
"github.com/pelletier/go-toml/v2"
parserv1 "github.com/stateful/runme/internal/gen/proto/go/runme/parser/v1"
"github.com/stateful/runme/internal/idgen"
"github.com/stateful/runme/internal/version"
"gopkg.in/yaml.v3"
)

const FrontmatterParsingVersion = "V2"

type RunmeMetaData struct {
ID string `protobuf:"bytes,1,opt,name=id,proto3"`
Version string `yaml:"version,omitempty"`
ID string `protobuf:"bytes,1,opt,name=id,proto3" yaml:"id,omitempty" json:"id,omitempty" toml:"id,omitempty"`
Version string `yaml:"version,omitempty"json:"version,omitempty" toml:"version,omitempty"`
}

type Frontmatter struct {
Expand All @@ -39,7 +38,7 @@ func NewFrontmatter() Frontmatter {
return Frontmatter{
Runme: RunmeMetaData{
ID: idgen.GenerateID(),
Version: FrontmatterParsingVersion,
Version: version.BuildVersion,
},
}
}
Expand All @@ -61,74 +60,83 @@ func (fpi FrontmatterParseInfo) Error() error {
}

// ParseFrontmatter extracts the Frontmatter from a raw string and identifies its format.
func ParseFrontmatter(raw string) (Frontmatter, FrontmatterParseInfo) {
var (
f Frontmatter
info FrontmatterParseInfo
err error
)

// Split the input string by new lines.
func ParseFrontmatter(raw string) (f Frontmatter, info FrontmatterParseInfo) {
lines := strings.Split(raw, "\n")

// Check for valid frontmatter delimiters.
if len(lines) < 2 || strings.TrimSpace(lines[0]) != strings.TrimSpace(lines[len(lines)-1]) {
info.other = errors.New("invalid frontmatter")
return f, info
info.other = errors.New("invalid frontmatter 1")
return
}

// Rejoin the lines to get the raw frontmatter content.
raw = strings.Join(lines[1:len(lines)-1], "\n")

// Attempt to unmarshal the frontmatter in various formats.
if err = yaml.Unmarshal([]byte(raw), &f); err == nil {
info.yaml = err
return f, info
bytes := []byte(raw)

if info.yaml = yaml.Unmarshal(bytes, &f); info.yaml == nil {
return
}

if err = json.Unmarshal([]byte(raw), &f); err == nil {
info.json = err
return f, info
if info.json = json.Unmarshal(bytes, &f); info.json == nil {
return
}

if err = toml.Unmarshal([]byte(raw), &f); err == nil {
info.toml = err
return f, info
if info.toml = toml.Unmarshal(bytes, &f); info.toml == nil {
return
}

// If unmarshaling fails for all formats, record the error.
info.other = err
return f, info
return
}

// StringifyFrontmatter converts Frontmatter to a string based on the provided format.
func StringifyFrontmatter(f Frontmatter, info FrontmatterParseInfo) (result string) {
var (
encodedBytes []byte
err error
)
func StringifyFrontmatter(raw string, f Frontmatter, info FrontmatterParseInfo) (result string) {
fmMap := make(map[string]interface{})
fmMap["runme"] = f.Runme

lines := strings.Split(raw, "\n")

if len(lines) < 2 || strings.TrimSpace(lines[0]) != strings.TrimSpace(lines[len(lines)-1]) {
info.other = errors.New("invalid frontmatter 2")
return
}

raw = strings.Join(lines[1:len(lines)-1], "\n")

bytes := []byte(raw)

switch {
case info.yaml != nil:
encodedBytes, err = yaml.Marshal(f)
case info.json != nil:
encodedBytes, err = json.Marshal(f)
case info.toml != nil:
encodedBytes, err = toml.Marshal(f)
default:
var buf bytes.Buffer
case info.yaml == nil:
if err := yaml.Unmarshal(bytes, &fmMap); err != nil {
return
}

var buf byteslib.Buffer
encoder := yaml.NewEncoder(&buf)
encoder.SetIndent(2)
err = encoder.Encode(&f)
encodedBytes = buf.Bytes()
_ = encoder.Encode(&fmMap)
bytes := buf.Bytes()
_ = encoder.Close()
}

if err == nil {
result = fmt.Sprintf("---\n%s---", string(encodedBytes))
result = fmt.Sprintf("---\n%s---", string(bytes))
return
case info.json == nil:
if err := json.Unmarshal(bytes, &fmMap); err != nil {
return
}

bytes, _ := json.Marshal(fmMap)
result = string(bytes)
return
case info.toml == nil:
if err := toml.Unmarshal(bytes, &fmMap); err != nil {
return
}

bytes, _ := toml.Marshal(fmMap)
result = fmt.Sprintf("+++\n%s+++", string(bytes))
return
default:
return ""
}

return
}

func (fmtr *Frontmatter) EnsureID() {
Expand All @@ -137,7 +145,7 @@ func (fmtr *Frontmatter) EnsureID() {
}

if fmtr.Runme.Version == "" {
fmtr.Runme.Version = FrontmatterParsingVersion
fmtr.Runme.Version = version.BuildVersion
}
}

Expand Down
15 changes: 15 additions & 0 deletions internal/idgen/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
var (
entropy io.Reader
entropyOnce sync.Once
generator func() string = DefaultGenerator
)

// DefaultEntropy returns a reader that generates ULID entropy.
Expand Down Expand Up @@ -56,8 +57,22 @@ func ValidID(id string) bool {

// GenerateID generates a new universal ID
func GenerateID() string {
return generator()
}

func DefaultGenerator() string {
entropy := DefaultEntropy()
now := time.Now()
ts := ulid.Timestamp(now)
return ulid.MustNew(ts, entropy).String()
}

func ResetGenerator() {
generator = DefaultGenerator
}

func MockGenerator(mockValue string) {
generator = func() string {
return mockValue
}
}
6 changes: 3 additions & 3 deletions pkg/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ func Test_codeBlockFrontmatter(t *testing.T) {
taskMemo[filepath.Base(task.GetFile())] = task
}

assert.Equal(t, taskMemo["BASH.md"].GetFrontmatter().Shell, "bash")
assert.Equal(t, taskMemo["KSH.md"].GetFrontmatter().Shell, "ksh")
assert.Equal(t, taskMemo["ZSH.md"].GetFrontmatter().Shell, "zsh")
assert.Equal(t, "bash", taskMemo["BASH.md"].GetFrontmatter().Shell)
assert.Equal(t, "ksh", taskMemo["KSH.md"].GetFrontmatter().Shell)
assert.Equal(t, "zsh", taskMemo["ZSH.md"].GetFrontmatter().Shell)
}

func Test_codeBlockSkipPromptsFrontmatter(t *testing.T) {
Expand Down

0 comments on commit be85975

Please sign in to comment.