Skip to content

Commit

Permalink
Guard against empty Runme metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout committed Feb 7, 2025
1 parent 5554e6a commit d4b29a7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
7 changes: 3 additions & 4 deletions internal/notebook/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ func (r *NotebookResolver) parseNotebook(context context.Context) (*parserv1.Not
continue
}

_, ok := cell.Metadata["id"]
if ok {
if _, ok := cell.Metadata["id"]; ok {
continue
}

if cell.Metadata == nil {
return nil, fmt.Errorf("cell metadata is missing")
if _, ok := cell.Metadata["runme.dev/id"]; !ok {
continue
}

cell.Metadata["id"] = cell.Metadata["runme.dev/id"]
Expand Down
40 changes: 36 additions & 4 deletions internal/notebook/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestResolveDaggerShell(t *testing.T) {
}

resolver := NewNotebookResolver(daggerShellNotebook)
declaration := `KERNEL_BINARY()
definition := `KERNEL_BINARY()
{
github.com/purpleclay/daggerverse/golang $(git https://github.com/stateful/runme | head | tree) \
| build \
Expand All @@ -73,9 +73,9 @@ EXTENSION()
`

expectedScripts := []string{
declaration + "KERNEL_BINARY\n",
declaration + "PRESETUP\n",
declaration + "EXTENSION\n",
definition + "KERNEL_BINARY\n",
definition + "PRESETUP\n",
definition + "EXTENSION\n",
}

for cellIndex, expectedScript := range expectedScripts {
Expand All @@ -84,3 +84,35 @@ EXTENSION()
assert.Equal(t, expectedScript, script)
}
}

func TestResolveDaggerShell_EmptyRunmeMetadata(t *testing.T) {
ctx := context.Background()

// fake notebook with dagger shell cells
daggerShellNotebook := &parserv1.Notebook{
Cells: []*parserv1.Cell{
{
Kind: parserv1.CellKind_CELL_KIND_CODE,
LanguageId: "sh",
Metadata: nil,
Value: "git github.com/stateful/runme |\n head |\n tree |\n file examples/README.md",
},
},
Metadata: map[string]string{
"runme.dev/frontmatter": "---\nshell: dagger shell\n---",
},
}

resolver := NewNotebookResolver(daggerShellNotebook)
stub := `{
git github.com/stateful/runme \
| head \
| tree \
| file examples/README.md
}
DAGGER_`

script, err := resolver.ResolveDaggerShell(ctx, uint32(0))
require.NoError(t, err)
require.Contains(t, script, stub)
}

0 comments on commit d4b29a7

Please sign in to comment.