From 2a4b7f4cd36aadd3448a303e6bcf1a703ad575d8 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Wed, 20 May 2026 17:21:49 -0400 Subject: cli cost: print more information --- .gitignore | 3 +++ Makefile | 29 ++++++++++++++++++++--------- cmd/lulu/cost.go | 20 ++++++++++++++++++-- cmd/lulu/test | 18 ++++++++++++------ cmd/lulu/testchecks/cost | 20 +++++++++++++++++++- cmd/lulu/testchecks/jobs | 2 ++ cmd/lulu/testchecks/orders | 2 -- cmd/lulu/tests/jobs | 1 + cmd/lulu/tests/orders | 1 - print_test.go | 9 +++++---- 10 files changed, 80 insertions(+), 25 deletions(-) create mode 100644 cmd/lulu/testchecks/jobs delete mode 100644 cmd/lulu/testchecks/orders create mode 100644 cmd/lulu/tests/jobs delete mode 100644 cmd/lulu/tests/orders diff --git a/.gitignore b/.gitignore index c8a5abb..5119e6d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,11 @@ +/.build /cmd/lulu/testerr /cmd/lulu/testout /.gen /lulu /spec.yml +/.testcli /testdata/clientkey /testdata/clientsecret +/.testpkg /todo diff --git a/Makefile b/Makefile index 624e561..8e573aa 100644 --- a/Makefile +++ b/Makefile @@ -4,25 +4,36 @@ SRC = $(filter-out ${GEN} ${TEST}, $(wildcard *.go)) all: build lulu -build: ${SRC} ${GEN} - go build +test: testpkg testcli -testcli: lulu - $(eval PATH=$(shell pwd):${PATH}) - cd cmd/lulu && ./test +build: .build +.build: ${SRC} ${GEN} + go build + touch $@ ${GEN}: .gen - .gen: ${SRC} - go generate && touch $@ + go generate + touch $@ lulu: ${SRC} ${GEN} $(wildcard cmd/lulu/*.go) go build ./cmd/$@ +testpkg: .testpkg +.testpkg: ${SRC} ${GEN} ${TEST} + go test ./... + touch $@ + +testcli: .testcli +.testcli: lulu $(wildcard $(addprefix cmd/lulu/, test tests/* testdata/* testchecks/*)) + $(eval PATH=$(shell pwd):${PATH}) + cd cmd/lulu && ./test + touch $@ + spec.yml: curl -L -o $@ 'https://api.lulu.com/api-docs/openapi-specs/openapi_public.yml' clean: - rm -rf ${GEN} lulu .gen cmd/lulu/testerr cmd/lulu/testout + rm -rf ${GEN} lulu .build .gen .testpkg .testcli cmd/lulu/testerr cmd/lulu/testout -.PHONY: testcli +.PHONY: all build test testpkg testcli diff --git a/cmd/lulu/cost.go b/cmd/lulu/cost.go index 8599340..93c3ab4 100644 --- a/cmd/lulu/cost.go +++ b/cmd/lulu/cost.go @@ -3,8 +3,10 @@ package main import ( "encoding/json" "fmt" + "os" "regexp" "strconv" + "text/tabwriter" "github.com/alecthomas/kong" @@ -38,8 +40,21 @@ func (cmd *CostCmd) Run(cli *kong.Kong, clnt *lulu.Client) error { if err != nil { return err } - // TODO: -v flag to print everything - fmt.Printf("total: %s %s\n", cost.TotalCostInclTax, cost.Currency) + + w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) + for _, fee := range cost.Fees { + fmt.Fprintf(w, "fee %s %s:\t%s %s\t\n", fee.Type, fee.Sku, fee.TotalCostExclTax, fee.Currency) + } + for i, item := range cost.LineItemCosts { + fmt.Fprintf(w, "item %d:\t%s %s\t\n", i, item.TotalCostExclTax, cost.Currency) + } + fmt.Fprintf(w, "shipping:\t%s %s\t\n", cost.ShipCost.TotalCostExclTax, cost.Currency) + fmt.Fprintf(w, "fulfillment:\t%s %s\t\n", cost.FulfillmentCost.TotalCostExclTax, cost.Currency) + fmt.Fprintf(w, "discount:\t%s %s\t\n", cost.TotalDiscount, cost.Currency) + fmt.Fprintf(w, "tax:\t%s %s\t\n", cost.TotalTax, cost.Currency) + fmt.Fprintf(w, "total:\t%s %s\t\n", cost.TotalCostInclTax, cost.Currency) + w.Flush() + if len(addrVal.Warnings) > 0 { j, err := json.Marshal(addrVal) if err != nil { @@ -47,6 +62,7 @@ func (cmd *CostCmd) Run(cli *kong.Kong, clnt *lulu.Client) error { } cli.Errorf("%s\n", j) } + return nil } diff --git a/cmd/lulu/test b/cmd/lulu/test index 8c29a2f..e446356 100755 --- a/cmd/lulu/test +++ b/cmd/lulu/test @@ -2,10 +2,13 @@ set -u +flags="-e -u" + [[ ! -d testout ]] && mkdir testout [[ ! -d testerr ]] && mkdir testerr -for test in tests/* +[[ $# -gt 0 ]] && tests="tests/$1" || tests=tests/* +for test in $tests do echo $test... base=${test##*/} @@ -14,7 +17,7 @@ do out=testout/$base err=testerr/$base if [ -f $want ]; then # check against expected output - sh $test >$out 2>$err + sh ${flags} $test >$out 2>$err if ! cmp -s $want $out then echo "FAIL $test: Actual" @@ -22,19 +25,22 @@ do exit 1 fi elif [ -f $check ]; then # use script to check output - sh $test >$out 2>$err + sh ${flags} $test >$out 2>$err echo "checking with $check..." >>$err - if ! sh $check $out >>$err + if ! sh ${flags} $check $out >>$err then echo "FAIL $test: check failed: $check" exit 1 fi - else # test should fail - if sh $test >$out 2>$err + elif [ -f $test ]; then # test should fail + if sh ${flags} $test >$out 2>$err then echo "FAIL $test: expected error; got \"$(cat $out)\"" exit 1 fi + else + echo "FAIL: does not exist: \"$test\"" + exit 1 fi done diff --git a/cmd/lulu/testchecks/cost b/cmd/lulu/testchecks/cost index 5ce1d48..479bdf4 100644 --- a/cmd/lulu/testchecks/cost +++ b/cmd/lulu/testchecks/cost @@ -1 +1,19 @@ -grep -E '^total: [1-9][0-9]*(\.[0-9]+)? [A-Z]{3}$' $1 +money='[0-9]+(\.[0-9]+)? [A-Z]{3}' +for field in "item 0" "item 1" "shipping" "fulfillment" "discount" "tax" "total" +do + re="$(printf '^%s: +%s *$' "${field}" "${money}")" + echo "$re" + grep -E "$re" $1 +done +grep -E -v '^item [^01]' $1 +awk ' + # Ensure sum of fields equals total. + !/^total/ { wantTotal += $(NF-1) } + /^total/ { + gotTotal = $(NF-1) + if (gotTotal > wantTotal+1e7 || gotTotal < wantTotal-1e7) { + printf "total (%f) does not match expected (%f)\n", gotTotal, wantTotal >>"/dev/stderr" + exit 1 + } + } +' $1 diff --git a/cmd/lulu/testchecks/jobs b/cmd/lulu/testchecks/jobs new file mode 100644 index 0000000..2d90544 --- /dev/null +++ b/cmd/lulu/testchecks/jobs @@ -0,0 +1,2 @@ +grep -E 'created +extid +id +orderid +nitems +total +status' $1 +grep -E '^ *[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z +.*[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+(\.[0-9]+)?[A-Z]{3} +[A-Z_]+$' $1 diff --git a/cmd/lulu/testchecks/orders b/cmd/lulu/testchecks/orders deleted file mode 100644 index 2d90544..0000000 --- a/cmd/lulu/testchecks/orders +++ /dev/null @@ -1,2 +0,0 @@ -grep -E 'created +extid +id +orderid +nitems +total +status' $1 -grep -E '^ *[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z +.*[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+(\.[0-9]+)?[A-Z]{3} +[A-Z_]+$' $1 diff --git a/cmd/lulu/tests/jobs b/cmd/lulu/tests/jobs new file mode 100644 index 0000000..8c6365d --- /dev/null +++ b/cmd/lulu/tests/jobs @@ -0,0 +1 @@ +lulu -s jobs diff --git a/cmd/lulu/tests/orders b/cmd/lulu/tests/orders deleted file mode 100644 index 910948c..0000000 --- a/cmd/lulu/tests/orders +++ /dev/null @@ -1 +0,0 @@ -lulu -s orders diff --git a/print_test.go b/print_test.go index 2ff6209..b5d87f4 100644 --- a/print_test.go +++ b/print_test.go @@ -224,10 +224,10 @@ func TestReprint(t *testing.T) { require.NoError(t, err) require.Len(t, job2.LineItems, 1) item1, item2 := job1.LineItems[0], job2.LineItems[0] - require.Equal(t, item1.Title, item2.Title) + require.Equal(t, item1.PrintableId, item2.PrintableId) require.Equal(t, item1.ExternalId, item2.ExternalId) + require.Equal(t, item1.Title, item2.Title) require.Equal(t, item1.Quantity, item2.Quantity) - require.Equal(t, item1.PrintableId, item2.PrintableId) require.Equal(t, item1.Mfg, item2.Mfg) } @@ -247,8 +247,9 @@ func TestGetPrintJob(t *testing.T) { job2, err := c.GetPrintJob(job1.Id) require.NoError(t, err) - // Ignore timestamp because job may have been modified between - // creation and retrieval time + // Ignore timestamp because job gets marked as 'modified' between + // creation and retrieval time even if it hasn't actually + // changed. job2.Modified = job1.Modified require.Equal(t, job1, job2) -- cgit v1.2.3