Skip to content

Commit

Permalink
test: fix tests and increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
g4s8 committed Dec 21, 2023
1 parent d239e70 commit 4b42316
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 78 deletions.
58 changes: 54 additions & 4 deletions _examples/complex.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,61 @@
<meta charset="utf-8">
<title>Environment Variables</title>
<style>
body {
font-family: sans-serif;
}
* {
box-sizing: border-box;
}
body {
font-family: sans-serif;
color: #1F2328;
}
article {
margin-left: auto;
margin-right: auto;
max-width: 1012px;
font-size: 16px;
line-height: 1.5;
word-wrap: break-word;
display: block;
}
section {
margin-top: 46px;
background-color: #ffffff;
border: 0px;
border-radius: 0px 0px 6px 6px;
padding: 0px;
min-width: 0px;
margin-top: 46px;
-moz-box-pack: center;
}
h1 {
padding-bottom: .3em;
font-size: 2em;
border-bottom: 1px solid hsla(210,18%,87%,1);
margin-bottom: 16px;
font-weight: 600;
line-height: 1.25;
}
li {
margin-top: .25em;
}
li code {
padding: .2em .4em;
margin: 0;
font-size: 85%;
white-space: break-sp#ffffffaces;
background-color: rgba(175,184,193,0.2);
border-radius: 6px;
}

li strong {
font-weight: 600;
}
</style>
</head>
<body>
<h1>Environment Variables</h1>
<section>
<article>
<h1>Environment Variables</h1>
<ul>
<li><code>SECRET</code> (from-file) - Secret is a secret value that is read from a file.</li>
<li><code>PASSWORD</code> (from-file, default: <code>/tmp/password</code>) - Password is a password that is read from a file.</li>
Expand All @@ -20,5 +68,7 @@ <h1>Environment Variables</h1>
<li><code>HOSTS</code> (separated by "<code>:</code>", <strong>required</strong>) - Hosts is a list of hosts.</li>
<li><code>WORDS</code> (comma-separated, from-file, default: <code>one,two,three</code>) - Words is just a list of words.</li>
</ul>
</article>
</section>
</body>
</html>
58 changes: 54 additions & 4 deletions _examples/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,67 @@
<meta charset="utf-8">
<title>Environment Variables</title>
<style>
body {
font-family: sans-serif;
}
* {
box-sizing: border-box;
}
body {
font-family: sans-serif;
color: #1F2328;
}
article {
margin-left: auto;
margin-right: auto;
max-width: 1012px;
font-size: 16px;
line-height: 1.5;
word-wrap: break-word;
display: block;
}
section {
margin-top: 46px;
background-color: #ffffff;
border: 0px;
border-radius: 0px 0px 6px 6px;
padding: 0px;
min-width: 0px;
margin-top: 46px;
-moz-box-pack: center;
}
h1 {
padding-bottom: .3em;
font-size: 2em;
border-bottom: 1px solid hsla(210,18%,87%,1);
margin-bottom: 16px;
font-weight: 600;
line-height: 1.25;
}
li {
margin-top: .25em;
}
li code {
padding: .2em .4em;
margin: 0;
font-size: 85%;
white-space: break-sp#ffffffaces;
background-color: rgba(175,184,193,0.2);
border-radius: 6px;
}

li strong {
font-weight: 600;
}
</style>
</head>
<body>
<h1>Environment Variables</h1>
<section>
<article>
<h1>Environment Variables</h1>
<ul>
<li><code>HOST</code> (separated by "<code>;</code>", <strong>required</strong>) - Hosts name of hosts to listen on.</li>
<li><code>PORT</code> (<strong>required</strong>, non-empty) - Port to listen on.</li>
<li><code>DEBUG</code> (default: <code>false</code>) - Debug mode enabled.</li>
</ul>
</article>
</section>
</body>
</html>
63 changes: 53 additions & 10 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"bytes"
"errors"
"path"
"testing"
)

Expand Down Expand Up @@ -41,15 +43,56 @@ func TestOptions(t *testing.T) {
})
}

type brokenWriter struct{}

var errBroken = errors.New("broken")

func (w brokenWriter) Write(p []byte) (n int, err error) {
return 0, errBroken
}

func TestGenerator(t *testing.T) {
g, err := newGenerator("stub", 1, withFormat("markdown"))
if err != nil {
t.Fatal("new generator error", err)
}
var out bytes.Buffer
err = g.generate(&out)
if err == nil {
t.Error("expected error, got nil")
}
t.Logf("got expected error: %v", err)
t.Run("broken-input", func(t *testing.T) {
g, err := newGenerator("stub", 1, withFormat("markdown"))
if err != nil {
t.Fatal("new generator error", err)
}
var out bytes.Buffer
err = g.generate(&out)
if err == nil {
t.Error("expected error, got nil")
}
t.Logf("got expected error: %v", err)
})
t.Run("broken-out", func(t *testing.T) {
src := path.Join(t.TempDir(), "example.go")
if err := copyTestFile("testdata/example_tags.go", src); err != nil {
t.Fatal("copy test file error", err)
}
g, err := newGenerator(src, 1, withFormat(""))
if err != nil {
t.Fatal("new generator error", err)
}
err = g.generate(brokenWriter{})
if err == nil {
t.Error("expected error, got nil")
}
})
t.Run("success", func(t *testing.T) {
src := path.Join(t.TempDir(), "example.go")
if err := copyTestFile("testdata/example_tags.go", src); err != nil {
t.Fatal("copy test file error", err)
}
g, err := newGenerator(src, 1, withFormat(""))
if err != nil {
t.Fatal("new generator error", err)
}
var out bytes.Buffer
if err := g.generate(&out); err != nil {
t.Fatal("generate error", err)
}
if out.Len() == 0 {
t.Error("expected output, got empty")
}
})
}
3 changes: 0 additions & 3 deletions inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ func (i *inspector) Visit(n ast.Node) ast.Visitor {
if line != i.execLine {
return nil
}
if !strings.HasPrefix(t.Text, "//go:generate") {
return nil
}

i.pendingType = true
return nil
Expand Down
32 changes: 17 additions & 15 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,21 @@ func TestInvalidConfig(t *testing.T) {
}

func TestMainRun(t *testing.T) {
inputFile := path.Join(t.TempDir(), "example.go")
if err := copyTestFile(path.Join("testdata", "example_type.go"), inputFile); err != nil {
t.Fatal("copy test file", err)
}
outputFile := path.Join(t.TempDir(), "example.md")
config := appConfig{
typeName: "Type1",
formatName: "markdown",
outputFileName: outputFile,
inputFileName: inputFile,
execLine: 0,
}
if err := run(&config); err != nil {
t.Fatal("run", err)
}
t.Run("ok", func(t *testing.T) {
inputFile := path.Join(t.TempDir(), "example.go")
if err := copyTestFile(path.Join("testdata", "example_type.go"), inputFile); err != nil {
t.Fatal("copy test file", err)
}
outputFile := path.Join(t.TempDir(), "example.md")
config := appConfig{
typeName: "Type1",
formatName: "markdown",
outputFileName: outputFile,
inputFileName: inputFile,
execLine: 0,
}
if err := run(&config); err != nil {
t.Fatal("run", err)
}
})
}
86 changes: 44 additions & 42 deletions render_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"bytes"
"strings"
"testing"
Expand Down Expand Up @@ -29,59 +30,60 @@ var testRenderItems = []renderItem{

func TestRender(t *testing.T) {
t.Run("markdown", testRenderer(tmplMarkdown, renderContext{Items: testRenderItems},
strings.Join([]string{
"# Environment Variables",
"",
"- `TEST_ENV` - This is a test environment variable.",
"- `TEST_ENV2` (comma-separated, default: `default value`) - This is another test environment variable.",
"- `TEST_ENV3` (**required**, expand, non-empty, from-file) - This is a third test environment variable.",
"",
}, "\n")))
"# Environment Variables",
"- `TEST_ENV` - This is a test environment variable.",
"- `TEST_ENV2` (comma-separated, default: `default value`) - This is another test environment variable.",
"- `TEST_ENV3` (**required**, expand, non-empty, from-file) - This is a third test environment variable."))
t.Run("plaintext", testRenderer(tmplPlaintext, renderContext{Items: testRenderItems},
strings.Join([]string{
"ENVIRONMENT VARIABLES",
"",
" * `TEST_ENV` - This is a test environment variable.",
" * `TEST_ENV2` (comma-separated, default: `default value`) - This is another test environment variable.",
" * `TEST_ENV3` (required, expand, non-empty, from-file) - This is a third test environment variable.",
"",
}, "\n")))
"ENVIRONMENT VARIABLES",
" * `TEST_ENV` - This is a test environment variable.",
" * `TEST_ENV2` (comma-separated, default: `default value`) - This is another test environment variable.",
" * `TEST_ENV3` (required, expand, non-empty, from-file) - This is a third test environment variable."))
t.Run("html", testRenderer(tmplHTML, renderContext{Items: testRenderItems},
strings.Join([]string{
`<!DOCTYPE html>`,
`<html lang="en">`,
` <head>`,
` <meta charset="utf-8">`,
` <title>Environment Variables</title>`,
` <style>`,
` body {`,
` font-family: sans-serif;`,
` }`,
` </style>`,
` </head>`,
` <body>`,
` <h1>Environment Variables</h1>`,
` <ul>`,
` <li><code>TEST_ENV</code> - This is a test environment variable.</li>`,
` <li><code>TEST_ENV2</code> (comma-separated, default: <code>default value</code>) - This is another test environment variable.</li>`,
` <li><code>TEST_ENV3</code> (<strong>required</strong>, expand, non-empty, from-file) - This is a third test environment variable.</li>`,
` </ul>`,
` </body>`,
`</html>`,
``,
}, "\n")))
`<!DOCTYPE html>`,
`<html lang="en">`,
`<head>`,
`<meta charset="utf-8">`,
`<title>Environment Variables</title>`,
`</head>`,
`<section>`,
`<article>`,
`<h1>Environment Variables</h1>`,
`<ul>`,
`<li><code>TEST_ENV</code> - This is a test environment variable.</li>`,
`<li><code>TEST_ENV2</code> (comma-separated, default: <code>default value</code>) - This is another test environment variable.</li>`,
`<li><code>TEST_ENV3</code> (<strong>required</strong>, expand, non-empty, from-file) - This is a third test environment variable.</li>`,
`</ul>`,
`</article>`,
`</section>`,
`</body>`,
`</html>`))
}

func testRenderer(tmpl template, c renderContext, expect string) func(*testing.T) {
func testRenderer(tmpl template, c renderContext, expectLines ...string) func(*testing.T) {
return func(t *testing.T) {
var buf bytes.Buffer
r := templateRenderer(tmpl)
err := r(c, &buf)
if err != nil {
t.Fatal(err)
}
if buf.String() != expect {
t.Errorf("expected %q, got %q", expect, buf.String())
scanner := bufio.NewScanner(&buf)
var currentLine int
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
expect := strings.TrimSpace(expectLines[currentLine])
if line == expect {
currentLine++
}
}
if err := scanner.Err(); err != nil {
t.Fatal("error reading output:", err)
}
if currentLine != len(expectLines) {
t.Log("output:")
t.Log(buf.String())
t.Fatalf("expected %d lines, got %d", len(expectLines), currentLine)
}
}
}
6 changes: 6 additions & 0 deletions testdata/example_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ type Type1 struct {
SecretKey string `env:"SECRET_KEY,required" json:"secret_key"`
// SecretVal is a secret value.
SecretVal string `json:"secret_val" env:"SECRET_VAL,notEmpty"`
// NotEnv is not an environment variable.
NotEnv string `json:"not_env"`
// NoTag is not tagged.
NoTag string
// BrokenTag is a tag that is broken.
BrokenTag string `env:"BROKEN_TAG,required`
}

0 comments on commit 4b42316

Please sign in to comment.