Skip to content

Commit

Permalink
fix(scenario): Allow for markdown files to not specify h1 headers (#208)
Browse files Browse the repository at this point in the history
h1 headers are currently used to denote the title of the scenario being
executed by IE and is currently made a requirement for the scenario to
be executed. This PR allows for markdown documents to not specify h1
tags by falling back to using the markdown file name as the title of the
scenario if no h1 tags are found within the document.
  • Loading branch information
vmarcella authored Jul 18, 2024
1 parent 37143a8 commit df9f444
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
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

0 comments on commit df9f444

Please sign in to comment.