-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f61a10e
commit 08dcccf
Showing
2 changed files
with
128 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package test | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var ( | ||
update = flag.Bool("update", false, "update .golden files") | ||
) | ||
|
||
var binaryName = "clibgen" | ||
var binaryPath = "" | ||
|
||
func TestMain(m *testing.M) { | ||
flag.Parse() | ||
|
||
err := os.Chdir("..") | ||
if err != nil { | ||
fmt.Printf("could not change dir: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
dir, err := os.Getwd() | ||
|
||
if err != nil { | ||
fmt.Printf("could not get current dir: %v", err) | ||
} | ||
|
||
binaryPath = filepath.Join(dir, binaryName) | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
|
||
func runBinary(args []string) ([]byte, error) { | ||
cmd := exec.Command(binaryPath, args...) | ||
cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata") | ||
return cmd.CombinedOutput() | ||
} | ||
|
||
func TestCliArgs(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []string | ||
fixture string | ||
}{ | ||
// {"no arguments", []string{}, "no-args.golden"}, | ||
{"help args", []string{"--help"}, "help.golden"}, | ||
// {"one argument", []string{"ciao"}, "one-argument.golden"}, | ||
// {"multiple arguments", []string{"ciao", "hello"}, "multiple-arguments.golden"}, | ||
// {"shout arg", []string{"--shout", "ciao"}, "shout-arg.golden"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
output, err := runBinary(tt.args) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
fmt.Println(string(output)) | ||
|
||
if *update { | ||
writeFixture(t, tt.fixture, output) | ||
} | ||
|
||
actual := string(output) | ||
|
||
expected := loadFixture(t, tt.fixture) | ||
|
||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("actual = %s, expected = %s", actual, expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func writeFixture(t *testing.T, goldenFile string, actual []byte) { | ||
t.Helper() | ||
goldenPath := "testdata/" + goldenFile | ||
|
||
f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644) | ||
defer f.Close() | ||
|
||
_, err = f.WriteString(string(actual)) | ||
|
||
if err != nil { | ||
t.Fatalf("Error writing to file %s: %s", goldenPath, err) | ||
} | ||
} | ||
|
||
func loadFixture(t *testing.T, goldenFile string) string { | ||
goldenPath := "testdata/" + goldenFile | ||
|
||
f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644) | ||
|
||
content, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
t.Fatalf("Error opening file %s: %s", goldenPath, err) | ||
} | ||
|
||
return string(content) | ||
} |
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,17 @@ | ||
|
||
Clibgen is a CLI application to search and download epubs, pdfs, from library genesis. | ||
Useful if you are lazy to open up a browser to download e-books/resources. | ||
|
||
Usage: | ||
clibgen [command] | ||
|
||
Available Commands: | ||
completion Generate the autocompletion script for the specified shell | ||
help Help about any command | ||
search search for a book, paper or article | ||
|
||
Flags: | ||
-h, --help help for clibgen | ||
-t, --toggle Help message for toggle | ||
|
||
Use "clibgen [command] --help" for more information about a command. |