Skip to content
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

fix(scenario): Allow for markdown files to not specify h1 headers #208

Merged
merged 2 commits into from
Jul 18, 2024
Merged
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
@@ -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)
53 changes: 53 additions & 0 deletions internal/engine/common/scenario_test.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

@@ -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
2 changes: 1 addition & 1 deletion internal/parsers/markdown.go
Original file line number Diff line number Diff line change
@@ -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