-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add more tests and coverate report
- Loading branch information
Showing
8 changed files
with
256 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestMarkdown(t *testing.T) { | ||
t.Run("header", func(t *testing.T) { | ||
var out bytes.Buffer | ||
mw := newMarkdownOutput(&out) | ||
mw.writeHeader() | ||
if err := mw.dump(); err != nil { | ||
t.Fatal("dump markdown writer", err) | ||
} | ||
if out.String() != "# Environment variables\n\n" { | ||
t.Fatal("unexpected output", out.String()) | ||
} | ||
}) | ||
t.Run("items", func(t *testing.T) { | ||
// envName string // environment variable name | ||
// doc string // field documentation text | ||
// flags docItemFlags | ||
// envDefault string | ||
var out bytes.Buffer | ||
mw := newMarkdownOutput(&out) | ||
mw.writeItem(docItem{ | ||
envName: "FOO", | ||
doc: "foo", | ||
}) | ||
mw.writeItem(docItem{ | ||
envName: "BAR", | ||
doc: "bar", | ||
envDefault: "1", | ||
}) | ||
mw.writeItem(docItem{ | ||
envName: "BAZ", | ||
doc: "baz", | ||
flags: docItemFlagRequired, | ||
}) | ||
mw.writeItem(docItem{ | ||
envName: "QUX", | ||
doc: "qux", | ||
flags: docItemFlagNonEmpty, | ||
}) | ||
mw.writeItem(docItem{ | ||
envName: "QUUX", | ||
doc: "quux", | ||
flags: docItemFlagRequired | docItemFlagNonEmpty, | ||
}) | ||
mw.writeItem(docItem{ | ||
envName: "CORGE", | ||
doc: "corge", | ||
flags: docItemFlagRequired | docItemFlagExpand, | ||
}) | ||
mw.writeItem(docItem{ | ||
envName: "GRAULT", | ||
doc: "grault", | ||
flags: docItemFlagFromFile | docItemFlagExpand | docItemFlagNonEmpty, | ||
}) | ||
if err := mw.dump(); err != nil { | ||
t.Fatal("dump markdown writer", err) | ||
} | ||
expectLines := []string{ | ||
"- `FOO` - foo", | ||
"- `BAR` (default: `1`) - bar", | ||
"- `BAZ` (**required**) - baz", | ||
"- `QUX` (not-empty) - qux", | ||
"- `QUUX` (**required**, not-empty) - quux", | ||
"- `CORGE` (**required**, expand) - corge", | ||
"- `GRAULT` (expand, not-empty, from file) - grault", | ||
} | ||
scanner := bufio.NewScanner(&out) | ||
var pos int | ||
for scanner.Scan() { | ||
expect := expectLines[pos] | ||
actual := scanner.Text() | ||
if actual != expect { | ||
t.Fatalf("unexpected output at line %d: %q != %q", pos, actual, expect) | ||
} | ||
pos++ | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package testdata | ||
|
||
//go:generate STUB | ||
type Type1 struct { | ||
// Foo stub | ||
Foo int `env:"FOO"` | ||
} | ||
|
||
type Type2 struct { | ||
// Baz stub | ||
Baz int `env:"BAZ"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package testdata | ||
|
||
type Type1 struct { | ||
// Secret is a secret value that is read from a file. | ||
Secret string `env:"SECRET,file"` | ||
// Password is a password that is read from a file. | ||
Password string `env:"PASSWORD,file" envDefault:"/tmp/password" json:"password"` | ||
// Certificate is a certificate that is read from a file. | ||
Certificate string `env:"CERTIFICATE,file,expand" envDefault:"${CERTIFICATE_FILE}"` | ||
// Key is a secret key. | ||
SecretKey string `env:"SECRET_KEY,required" json:"secret_key"` | ||
// SecretVal is a secret value. | ||
SecretVal string `json:"secret_val" env:"SECRET_VAL,notEmpty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package testdata | ||
|
||
type Type1 struct { | ||
// Foo stub | ||
Foo int `env:"FOO"` | ||
} | ||
|
||
type Type2 struct { | ||
// Baz stub | ||
Baz int `env:"BAZ"` | ||
} |