Skip to content

Commit

Permalink
Adding basic output tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed May 6, 2024
1 parent 4901995 commit 216583d
Show file tree
Hide file tree
Showing 32 changed files with 8,112 additions and 146 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Lint
on:
push:
tags:
- v*
branches:
- main
pull_request:
jobs:
lint:
name: golangci-lint
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3

- uses: actions/setup-go@v4
with:
go-version-file: "go.mod"

- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.55
30 changes: 30 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Automatically tag a merge with main.
#

name: Release

on:
push:
branches:
- main
paths-ignore:
- "README.md"

jobs:
release:
name: Tag
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: "0" # make sure we get all commits!

- name: Bump version and push tag
id: bump
uses: anothrNick/github-tag-action@1.52.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_BRANCHES: release
WITH_V: true
24 changes: 24 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test Go
on: [push]
jobs:
test:
name: Test
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: "go.mod"
id: go

- name: Install Dependencies
env:
GOPROXY: https://proxy.golang.org,direct
run: go mod download

- name: Test
run: go test -tags unit -race ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# vendor/

*.hide.json
*.hide.html

# Go workspace file
go.work
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Released under the Apache 2.0 [LICENSE](https://github.com/invopop/gobl/blob/mai

### Generate Templates

GOBL HTML uses [templ](https://templ.guide/) to define a set of components in Go. To genera the templates, run:
GOBL HTML uses [templ](https://templ.guide/) to define a set of components in Go. To generate the templates, run:

```bash
templ generate
Expand All @@ -19,3 +19,11 @@ During development, it can help massive to have hot reload to be able to make ch
```bash
templ generate --watch --cmd="go run ./cmd/gobl.html serve --pdf prince"
```

### Testing

Tests are currently pretty limited. To ensure the basics are covered, the contents of the `examples` directory are converted to HTML, pretty printed, and output to the `examples/out` directory. The tests will ensure the output is as expected. To update the output test data run:

```bash
go test ./... --update
```
7 changes: 0 additions & 7 deletions cmd/gobl.html/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
"os"
"os/signal"
"syscall"

"github.com/invopop/ctxi18n"
"github.com/invopop/gobl.html/locales"
)

// build data provided by goreleaser and mage setup
Expand All @@ -28,10 +25,6 @@ func run() error {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()

if err := ctxi18n.LoadWithDefault(locales.Content, "en"); err != nil {
return fmt.Errorf("loading locales: %w", err)
}

return root().cmd().ExecuteContext(ctx)
}

Expand Down
14 changes: 7 additions & 7 deletions components/bill/invoice/invoice.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ import (
"github.com/invopop/gobl/org"
"github.com/invopop/gobl/tax"
"github.com/invopop/gobl.html/components/t"

"github.com/invopop/gobl.html/components/regimes/es"
"github.com/invopop/gobl.html/components/regimes/co"
"github.com/invopop/gobl.html/components/regimes/mx"
"github.com/invopop/gobl.html/internal"
)

// Invoice renders a complete GOBL bill.Invoice object.
templ Invoice(env *gobl.Envelope, inv *bill.Invoice) {
<style type="text/css">
article.invoice {
}
header {
width: 100%;
display: flex;
Expand Down Expand Up @@ -55,8 +53,6 @@ templ Invoice(env *gobl.Envelope, inv *bill.Invoice) {
max-width: 100%;
/* max-height: 10mm; */
}
section.title h1.type {
}
section.title h2.code {
font-size: 12pt;
color: #030712;
Expand Down Expand Up @@ -318,7 +314,7 @@ templ Invoice(env *gobl.Envelope, inv *bill.Invoice) {
templ title(inv *bill.Invoice) {
<section class="title">
<div class="hero">
if img := supplierLogo(inv); img != nil {
if img := supplierLogo(ctx, inv); img != nil {
<img src={ img.URL } alt={ img.Label } height={ logoHeight(img) }/>
} else {
<div class="alias">
Expand Down Expand Up @@ -350,7 +346,11 @@ func supplierAlias(inv *bill.Invoice) string {
return inv.Supplier.Name
}

func supplierLogo(inv *bill.Invoice) *org.Image {
func supplierLogo(ctx context.Context, inv *bill.Invoice) *org.Image {
opts := internal.Options(ctx)
if opts.Logo != nil {
return opts.Logo
}
if len(inv.Supplier.Logos) > 0 {
return inv.Supplier.Logos[0]
}
Expand Down
20 changes: 12 additions & 8 deletions components/bill/invoice/invoice_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/bill/invoice/summary.templ
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ templ summaryOrderPeriodRows(p *cal.Period) {
}
</span>
<span class="value">
@t.T(".order_period_range", i18n.M{"start": p.Start, "end": p.End})
@t.T(".order_period_range", i18n.M{"start": t.Localize(ctx, p.Start), "end": t.Localize(ctx, p.End)})
</span>
</li>
}
Expand Down
2 changes: 1 addition & 1 deletion components/bill/invoice/summary_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions components/footer.templ
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/invopop/gobl"
"github.com/invopop/gobl.html/components/images"
"github.com/invopop/ctxi18n/i18n"
"github.com/invopop/gobl.html/internal"
)

templ footerPrint(env *gobl.Envelope) {
Expand Down Expand Up @@ -83,9 +84,9 @@ templ footerPage() {
}

templ footerNotes(env *gobl.Envelope) {
if env.Head.Notes != "" {
if txt := footerNotesText(ctx, env); txt != "" {
<span class="notes">
@templ.Raw(env.Head.Notes)
@templ.Raw(txt)
</span>
}
}
Expand All @@ -105,3 +106,13 @@ func pageNumber(ctx context.Context) string {
})
return txt
}

func footerNotesText(ctx context.Context, env *gobl.Envelope) string {
if opts := internal.Options(ctx); opts.Notes != "" {
return opts.Notes
}
if env.Head.Notes != "" {
return env.Head.Notes
}
return ""
}
15 changes: 13 additions & 2 deletions components/footer_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions components/org/party.templ
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org

import (
"cmp"
"slices"
"strings"

"github.com/invopop/gobl/l10n"
Expand Down Expand Up @@ -72,8 +74,8 @@ templ identities(idents []*org.Identity) {
}

templ partyExtensions(party *org.Party) {
for k, v := range party.Ext {
if txt := mapPartyExtension(ctx, k, v); txt != "" {
for _, k := range extensionKeys(party.Ext) {
if txt := mapPartyExtension(ctx, k, party.Ext[k]); txt != "" {
<div class="party-extension">
{ txt }
</div>
Expand Down Expand Up @@ -145,3 +147,14 @@ func mapPartyExtension(ctx context.Context, k cbc.Key, v tax.ExtValue) string {
}
return ""
}

func extensionKeys(ext tax.Extensions) []cbc.Key {
keys := make([]cbc.Key, 0, len(ext))
for k := range ext {
keys = append(keys, k)
}
slices.SortFunc(keys, func(a, b cbc.Key) int {
return cmp.Compare(a.String(), b.String())
})
return keys
}
Loading

0 comments on commit 216583d

Please sign in to comment.