Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/engine/common/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,16 @@ func CreateScenarioFromMarkdown(

// Group the code blocks into steps.
steps := groupCodeBlocksIntoSteps(codeBlocks)

// If no title is found, we simply use the name of the markdown file as
// the title of the scenario.
title, err := parsers.ExtractScenarioTitleFromAst(markdown, source)
if err != nil {
return nil, err
logging.GlobalLogger.Warnf(
"Failed to extract scenario title: '%s'. Using the name of the markdown as the scenario title",
err,
)
title = filepath.Base(path)
}

logging.GlobalLogger.Infof("Successfully built out the scenario: %s", title)
Expand Down
53 changes: 53 additions & 0 deletions internal/engine/common/scenario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -79,6 +80,58 @@ func TestResolveMarkdownSource(t *testing.T) {
})
}

func TestScenarioParsing(t *testing.T) {
// Test parsing a scenario from markdown
t.Run("Parse scenario that doesn't have an h1 tag to use for it's title", func(t *testing.T) {
content := "Test content from local file"
temporaryFile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatalf("Error creating temporary file: %v", err)
}
defer os.Remove(temporaryFile.Name())

if _, err := temporaryFile.Write([]byte(content)); err != nil {
t.Fatalf("Error writing to temporary file: %v", err)
}
if err := temporaryFile.Close(); err != nil {
t.Fatalf("Error closing temporary file: %v", err)
}

path := temporaryFile.Name()

scenario, err := CreateScenarioFromMarkdown(path, []string{"bash"}, nil)

assert.NoError(t, err)
fmt.Println(scenario)
assert.Equal(t, filepath.Base(path), scenario.Name)
})

// Test parsing a scenario from markdown
t.Run("Parse scenario that does have an h1 tag to use for it's title", func(t *testing.T) {
content := "# Scenario-title\nTest content from local file"
temporaryFile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatalf("Error creating temporary file: %v", err)
}
defer os.Remove(temporaryFile.Name())

if _, err := temporaryFile.Write([]byte(content)); err != nil {
t.Fatalf("Error writing to temporary file: %v", err)
}
if err := temporaryFile.Close(); err != nil {
t.Fatalf("Error closing temporary file: %v", err)
}

path := temporaryFile.Name()

scenario, err := CreateScenarioFromMarkdown(path, []string{"bash"}, nil)

assert.NoError(t, err)
fmt.Println(scenario)
assert.Equal(t, "Scenario-title", scenario.Name)
})
}

func TestVariableOverrides(t *testing.T) {
variableScenarioPath := "../../../scenarios/testing/variables.md"
// Test overriding environment variables
Expand Down
2 changes: 1 addition & 1 deletion internal/parsers/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func ExtractScenarioTitleFromAst(node ast.Node, source []byte) (string, error) {
})

if header == "" {
return "", fmt.Errorf("no header found")
return "", fmt.Errorf("no h1 header found to use as the scenario title")
}

return header, nil
Expand Down