Skip to content

Commit

Permalink
Merge pull request googlecodelabs#396 from KeyboardNerd/b_391
Browse files Browse the repository at this point in the history
Fix duplicated exported content with memory export
  • Loading branch information
shawnbuso authored Mar 20, 2020
2 parents 66dfb74 + 6f628b6 commit 3e78ec0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
15 changes: 6 additions & 9 deletions claat/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cmd

import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -169,16 +170,12 @@ func writeCodelabWriter(w io.Writer, clab *types.Codelab, extraVars map[string]s
Steps: clab.Steps,
Extra: extraVars,
}}
for i, step := range clab.Steps {
data.Current = step
data.StepNum = i + 1
data.Prev = i > 0
data.Next = i < len(clab.Steps)-1
if err := render.Execute(w, ctx.Format, data); err != nil {
return err
}

if ctx.Format == "offline" {
return errors.New("exporting codelab offline is not supported for In-Memory Export")
}
return nil

return render.Execute(w, ctx.Format, data)
}

// writeCodelab stores codelab main content in ctx.Format and its metadata
Expand Down
43 changes: 35 additions & 8 deletions claat/cmd/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"path"
"reflect"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -36,21 +37,15 @@ func TestExportCodelabMemory(t *testing.T) {
tests := []struct {
name string
filePath string
knownBug string
}{
{
name: "Multiple Steps",
filePath: "testdata/simple-2-steps.md",
knownBug: "https://github.com/googlecodelabs/tools/issues/391",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.knownBug != "" {
t.Skip("Skipping Tests with known issue", test.knownBug)
}

tmp, err := ioutil.TempDir(".", "TestExportCodelabMemory-*")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -105,9 +100,41 @@ func TestExportCodelabMemory(t *testing.T) {
t.Errorf("ExportCodelabMemory returns metadata:\n%+v\nwant:\n%+v\n", gotMeta, wantMeta)
}

if bytes.Compare(wantBytes, gotBytes.Bytes()) != 0 {
t.Errorf("ExportCodelabMemory returns diff: %s", cmp.Diff(string(wantBytes), string(gotBytes.Bytes())))
wantContent := filterIgnoredLinePrefix(string(wantBytes))
gotContent := filterIgnoredLinePrefix(string(gotBytes.Bytes()))
if diff := cmp.Diff(wantContent, gotContent); diff != "" {
t.Errorf("ExportCodelabMemory returns diff: %s", diff)
}
})
}
}

func filterIgnoredLinePrefix(content string) string {
// ignoredLinePrefix is used because
// 1. InMemory Export method doesn't have a file to begin with
// 2. Some expected bugs to be resolved.
ignoredLinePrefix := []string{
"<meta name=\"original_source\" content=\"",
"doc-id=\"",
"last-updated=\"", // https://github.com/googlecodelabs/tools/issues/395
}

lines := strings.Split(content, "\n")
processedContent := []string{}
for _, l := range lines {
trimmed := strings.TrimLeft(l, " ")
toBeIgnored := false
for _, ignored := range ignoredLinePrefix {
if strings.HasPrefix(trimmed, ignored) {
toBeIgnored = true
break
}
}

if !toBeIgnored {
processedContent = append(processedContent, l)
}
}

return strings.Join(processedContent, "\n")
}

0 comments on commit 3e78ec0

Please sign in to comment.