Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/setup versioning #35

Merged
merged 7 commits into from
Aug 16, 2023
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
dist
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ builds:
- linux
- windows
- darwin
ldflags:
- -s -w -X github.com/laureanray/clibgen/cmd.version={{.Version}}
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
14 changes: 13 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var version string

var rootCmd = &cobra.Command{
Use: "clibgen",
Short: "Library Genesis command line / terminal client",
Expand All @@ -14,6 +17,15 @@ Clibgen is a CLI application to search and download epubs, pdfs, from library ge
Useful if you are lazy to open up a browser to download e-books/resources.`,
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Hugo",
Long: `All software has versions. This is Hugo's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
Expand All @@ -22,5 +34,5 @@ func Execute() {
}

func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.AddCommand(versionCmd)
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "github.com/laureanray/clibgen/cmd"
import (
"github.com/laureanray/clibgen/cmd"
)

func main() {
cmd.Execute()
Expand Down
80 changes: 0 additions & 80 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ import (
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)

var (
update = flag.Bool("update", false, "update .golden files")
)

var binaryName = "clibgen"
var binaryPath = ""

Expand All @@ -42,12 +37,6 @@ func TestMain(m *testing.M) {
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 runBinaryWithFileInput(args []string, bytesToWrite []byte) ([]byte, error) {
fmt.Println("Running binary with file input", bytesToWrite)
fmt.Printf("Executing command: %s", binaryPath)
Expand Down Expand Up @@ -99,38 +88,6 @@ func runBinaryWithFileInput(args []string, bytesToWrite []byte) ([]byte, error)
return out, err
}

func TestStaticCliArgs(t *testing.T) {
tests := []struct {
name string
args []string
fixture string
}{
{"help args", []string{"--help"}, "help.golden"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output, err := runBinary(tt.args)

if err != nil {
t.Fatal(err)
}

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 TestSearch(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -158,40 +115,3 @@ func TestSearch(t *testing.T) {
})
}
}

func removeLastLine(str string, num int) string {
lines := strings.Split(str, "\n")

if len(lines) > 0 {
lines = lines[:len(lines)-num]
}

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

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)
}
17 changes: 0 additions & 17 deletions testdata/help.golden

This file was deleted.