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

Ledger cli command line #164

Closed
wants to merge 3 commits into from
Closed
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
112 changes: 112 additions & 0 deletions ledger-cli/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"flag"
"fmt"
"io"
"os"
"os/exec"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/darcys22/godbledger/godbledger/cmd"
"github.com/darcys22/godbledger/tests/components"
"github.com/darcys22/godbledger/tests/helpers"
e2e "github.com/darcys22/godbledger/tests/params"

"github.com/urfave/cli/v2"
)

func copyAndCapture(w io.Writer, r io.Reader) ([]byte, error) {
var out []byte
buf := make([]byte, 1024)
for {
n, err := r.Read(buf[:])
if n > 0 {
d := buf[:n]
out = append(out, d...)
_, err := w.Write(d)
if err != nil {
return out, err
}
}
if err != nil {
// Read returns io.EOF at the end of file, which is not an error for us
if err == io.EOF {
err = nil
}
return out, err
}
}
}

func TestCommandLine(t *testing.T) {
// Create a config from the defaults which would usually be created by the CLI library
set := flag.NewFlagSet("test", 0)
set.String("config", "", "doc")
ctx := cli.NewContext(nil, set, nil)
err, cfg := cmd.MakeConfig(ctx)
if err != nil {
t.Fatalf("New Config Failed: %v", err)
}

// Set the Database type to a SQLite3 in memory database
cfg.DatabaseType = "memorydb"

goDBLedgerPID := components.StartGoDBLedger(t, cfg, e2e.LogFileName, 0)
defer helpers.KillProcesses(t, []int{goDBLedgerPID})

time.Sleep(time.Duration(1) * time.Second)
logfileName := fmt.Sprintf("%s-%d", e2e.LogFileName, 0)
logFile, err := os.Open(logfileName)
assert.NoError(t, err)
defer helpers.DeleteLogFiles(t, []*os.File{logFile})

if err := helpers.WaitForTextInFile(logFile, "Starting GoDBLedger Server"); err != nil {
t.Fatalf("failed to find GoDBLedger start in logfile: %s, this means the server did not start: %v", logfileName, err)
}

// Start up ledger CLI and use the journal wizard
cmd := exec.Command("../build/bin/native/ledger-cli", "journal")
var stdout, stderr []byte
var errStdout, errStderr error
stdoutIn, _ := cmd.StdoutPipe()
stdin, err := cmd.StdinPipe()
assert.NoError(t, err)
err = cmd.Start()
assert.NoError(t, err)

var wg sync.WaitGroup
wg.Add(1)
defer stdin.Close()
go func() {
io.WriteString(stdin, "2020-06-30\n")
io.WriteString(stdin, "something\n")
io.WriteString(stdin, "something\n")
io.WriteString(stdin, "account1\n")
io.WriteString(stdin, "300\n")
io.WriteString(stdin, "y\n")
io.WriteString(stdin, "something\n")
io.WriteString(stdin, "account2\n")
io.WriteString(stdin, "-300\n")
io.WriteString(stdin, "n\n")
stdout, errStdout = copyAndCapture(os.Stdout, stdoutIn)
wg.Done()
}()
err = cmd.Wait()
assert.NoError(t, err)
wg.Wait()

if errStdout != nil || errStderr != nil {
t.Fatal("failed to capture stdout or stderr\n")
}
outStr, errStr := string(stdout), string(stderr)
t.Logf("\nout:\n%s\nerr:\n%s\n", outStr, errStr)

//TODO capture the success message
//INFO ledger-cli: Add Transaction Response: c3jcc0m49b9ih5ol3e80

}
1 change: 1 addition & 0 deletions tests/components/godbledger_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ func StartGoDBLedger(t *testing.T, config *cmd.LedgerConfig, logfilename string,
defer stdOutFile.Close()

return cmd.Process.Pid

}
3 changes: 2 additions & 1 deletion utils/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ func doDebianSource(cmdline []string) {
maybeSkipArchive(env)

// Import the signing key.
//gpg --export-secret-key [email protected] | base64 | paste -s -d '' > secret-key-base64-encoded.gpg
//gpg --export-secret-key [email protected] | base64 | paste -s -d '' > secret-signing-key-base64-encoded.gpg
if key := getenvBase64("PPA_SIGNING_KEY"); len(key) > 0 {
gpg := exec.Command("gpg", "--import", "--no-tty", "--batch", "--yes")
gpg.Stdin = bytes.NewReader(key)
Expand Down Expand Up @@ -759,6 +759,7 @@ func ppaUpload(workdir, ppa, sshUser string, files []string) {
incomingDir := fmt.Sprintf("~%s/ubuntu/%s", p[0], p[1])
// Create the SSH identity file if it doesn't exist.
var idfile string
//cat ppakey | base64 | paste -s -d '' > secret-ssh-key-base64-encoded
if sshkey := getenvBase64("PPA_SSH_KEY"); len(sshkey) > 0 {
idfile = filepath.Join(workdir, "sshkey")
if _, err := os.Stat(idfile); os.IsNotExist(err) {
Expand Down