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
27 changes: 4 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// markdown is rendered, so they can be kept in the file as pointers
// to the origin of the embedded text.
//
// The command receives a list of markdown files, if none is given it
// reads from the standard input.
// The command receives a list of markdown files to process. At least one
// file must be provided; reading from standard input is not supported.
//
// embedmd supports two flags:
// -d: will print the difference of the input file with what the output
Expand Down Expand Up @@ -77,34 +77,15 @@ func main() {
}
}

var (
stdout io.Writer = os.Stdout
stdin io.Reader = os.Stdin
)
var stdout io.Writer = os.Stdout

func embed(paths []string, rewrite, doDiff bool) (foundDiff bool, err error) {
if rewrite && doDiff {
return false, fmt.Errorf("error: cannot use -w and -d simultaneously")
}

if len(paths) == 0 {
if rewrite {
return false, fmt.Errorf("error: cannot use -w with standard input")
}
if !doDiff {
return false, embedmd.Process(stdout, stdin)
}

var out, in bytes.Buffer
if err := embedmd.Process(&out, io.TeeReader(stdin, &in)); err != nil {
return false, err
}
d, err := diff(in.String(), out.String())
if err != nil || len(d) == 0 {
return false, err
}
fmt.Fprintf(stdout, "%s", d)
return true, nil
return false, fmt.Errorf("error: no markdown files provided")
}

for _, path := range paths {
Expand Down
55 changes: 8 additions & 47 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,63 +23,24 @@ import (
"github.com/campoy/embedmd/internal/testutil"
)

func TestEmbedStreams(t *testing.T) {
func TestEmbedNoPaths(t *testing.T) {
tc := []struct {
name string
in, out string
err string
d, w bool
foundDiff bool
name string
err string
d, w bool
}{
{name: "just some text",
in: "# hello\ntest\n",
out: "# hello\ntest\n",
},
{name: "can't rewrite stdin",
w: true,
err: "error: cannot use -w with standard input",
{name: "no files provided",
err: "error: no markdown files provided",
},
{name: "can't diff and rewrite",
w: true, d: true,
err: "error: cannot use -w and -d simultaneously",
},
{name: "empty diff",
d: true,
in: "# hello\ntest\n",
foundDiff: false,
},
{name: "non empty diff",
d: true,
in: "# hello\ntest",
out: `@@ -1,2 +1,3 @@
# hello
test
+
`,
foundDiff: true,
},
}

defer func(r io.Reader, w io.Writer) { stdin, stdout = r, w }(stdin, stdout)

for _, tt := range tc {
stdin = strings.NewReader(tt.in)
buf := &bytes.Buffer{}
stdout = buf
foundDiff, err := embed(nil, tt.w, tt.d)
if !testutil.EqErr(t, tt.name, err, tt.err) {
continue
}
if got := buf.String(); tt.out != got {
t.Errorf("case [%s] expected output\n%q\n; got\n%q", tt.name, tt.out, got)
}
if tt.d && foundDiff != tt.foundDiff {
if foundDiff {
t.Errorf("case [%s] expected to find a diff, but didn't", tt.name)
} else {
t.Errorf("case [%s] didn't expect to find a diff, but did", tt.name)
}
}
_, err := embed(nil, tt.w, tt.d)
testutil.EqErr(t, tt.name, err, tt.err)
}
}

Expand Down
Loading