Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lesomnus committed Nov 30, 2024
1 parent 39e773b commit 484f096
Show file tree
Hide file tree
Showing 41 changed files with 957 additions and 365 deletions.
22 changes: 20 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,31 @@ on:
- "!**/*_test.go"
- ".dockerignore"
- "Dockerfile"
pull_request:
branches:
- main
paths:
- "**/*.go"
- "!**/*_test.go"
- ".dockerignore"
- "Dockerfile"
workflow_dispatch: {}

jobs:
build:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.23.3"

- name: Test
run: |
go test ./...
go build -o bring
bring version
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3

Expand All @@ -31,7 +49,7 @@ jobs:
run: |
BRING_VERSION="$(gh release list --exclude-drafts --json tagName --jq '.[0].tagName')"
BRING_VERSION+="+{{ github.run_id }}"
echo "version=$BRING_VERSION" | tee >> $GITHUB_OUTPUT
echo "version=$BRING_VERSION" | tee -a $GITHUB_OUTPUT
./scripts/gen-version-file.sh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -52,4 +70,4 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
push: true
push: ${{ github.event_name == "push" }}
File renamed without changes.
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"cwd": "${workspaceFolder}",
"args": [
"--into",
"inventory"
]
}
]
}
7 changes: 1 addition & 6 deletions bringer/bringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"log/slog"
"net/url"

"github.com/lesomnus/bring/thing"
Expand All @@ -17,12 +16,8 @@ type Bringer interface {
func FromUrl(u url.URL, opts ...Option) (Bringer, error) {
bf, ok := bringers[u.Scheme]
if !ok {
return nil, fmt.Errorf("scheme %s not supported", u.Scheme)
return nil, fmt.Errorf("scheme not supported: %s", u.Scheme)
}

return bf(opts...), nil
}

func name(name string) slog.Attr {
return slog.String("bringer", name)
}
32 changes: 0 additions & 32 deletions bringer/file.go

This file was deleted.

38 changes: 38 additions & 0 deletions bringer/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package file

import (
"context"
"fmt"
"io"
"log/slog"
"os"

"github.com/lesomnus/bring/bringer"
"github.com/lesomnus/bring/log"
"github.com/lesomnus/bring/thing"
)

type br struct{}

func FileBringer(opts ...bringer.Option) bringer.Bringer {
return &br{}
}

func (b *br) Bring(ctx context.Context, t thing.Thing, opts ...bringer.Option) (io.ReadCloser, error) {
l := log.From(ctx).With(slog.String("bringer", "file"))

p := fmt.Sprintf("%s%s", t.Url.Host, t.Url.Path)
l.Info("open", slog.String("path", p))

f, err := os.Open(p)
if err != nil {
return nil, fmt.Errorf("open: %w", err)
}

return f, nil
}

func init() {
bringer.Register("", FileBringer)
bringer.Register("file", FileBringer)
}
89 changes: 89 additions & 0 deletions bringer/file/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package file_test

import (
"context"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"testing"

"github.com/lesomnus/bring/bringer/file"
"github.com/lesomnus/bring/thing"
"github.com/stretchr/testify/require"
)

func TestFileBringer(t *testing.T) {
b := file.FileBringer()

d := t.TempDir()
p := filepath.Join(d, "foo")
data := []byte("bar")
err := os.WriteFile(p, data, 0o644)
require.NoError(t, err)

t.Run("absolute path without schema", func(t *testing.T) {
require := require.New(t)

u, err := url.Parse(p)
if err != nil {
require.NoError(err)
}

f, err := b.Bring(context.Background(), thing.Thing{Url: *u})
if err == nil {
defer f.Close()
}
require.NoError(err)

v, err := io.ReadAll(f)
require.NoError(err)
require.Equal(v, data)
})
t.Run("absolute path with schema", func(t *testing.T) {
require := require.New(t)

u, err := url.Parse(fmt.Sprintf("file://%s", p))
if err != nil {
require.NoError(err)
}

f, err := b.Bring(context.Background(), thing.Thing{Url: *u})
if err == nil {
defer f.Close()
}
require.NoError(err)

v, err := io.ReadAll(f)
require.NoError(err)
require.Equal(v, data)
})
t.Run("relative path", func(t *testing.T) {
require := require.New(t)

wd, err := os.Getwd()
if err != nil {
panic(err)
}
if err := os.Chdir(d); err != nil {
panic(err)
}
defer os.Chdir(wd)

u, err := url.Parse("./foo")
if err != nil {
require.NoError(err)
}

f, err := b.Bring(context.Background(), thing.Thing{Url: *u})
if err == nil {
defer f.Close()
}
require.NoError(err)

v, err := io.ReadAll(f)
require.NoError(err)
require.Equal(v, data)
})
}
55 changes: 0 additions & 55 deletions bringer/http.go

This file was deleted.

91 changes: 91 additions & 0 deletions bringer/http/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package http

import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"

"github.com/lesomnus/bring/bringer"
"github.com/lesomnus/bring/log"
"github.com/lesomnus/bring/thing"
)

type br struct {
client http.Client
}

func (b *br) apply(opts []bringer.Option) {
for _, opt := range opts {
switch o := opt.(type) {
case *transportOption:
b.client.Transport = o.Value
}
}
}

func (b *br) with(opts []bringer.Option) *br {
if len(opts) == 0 {
return b
}

b_ := *b
b_.apply(opts)
return &b_
}

func HttpBringer(opts ...bringer.Option) bringer.Bringer {
b := &br{}
b.apply(opts)
return b
}

func (b *br) Bring(ctx context.Context, t thing.Thing, opts ...bringer.Option) (io.ReadCloser, error) {
l := log.From(ctx).With(slog.String("bringer", "http"))
b = b.with(opts)

// TODO: check ETag, Cache-Control, of Last-Modified header.
// res, err := http.Head(t.Url.String())
// if err != nil {
// return nil, fmt.Errorf("request head: %w", err)
// }

// TODO: do not buffer the response in the memory according to the config.
// f, err := os.CreateTemp("", "bring-")
// if err != nil {
// return nil, fmt.Errorf("create temp file: %w", err)
// }
// defer f.Close()

l.Info("request GET")
res, err := b.client.Get(t.Url.String())
if err != nil {
e := &url.Error{}
if errors.As(err, &e) {
return nil, err
}
return nil, fmt.Errorf("request GET: %w", err)
}
l.Info("response", slog.Int("status", res.StatusCode))
if l.Enabled(ctx, slog.LevelDebug) {
l.Debug("response", slog.String("header", fmt.Sprintf("%v", res.Header)))
}
if res.StatusCode != http.StatusOK {
if l.Enabled(ctx, slog.LevelDebug) {
if data, err := io.ReadAll(res.Body); err == nil {
l.Debug("response", slog.String("body", string(data)))
}
}
return nil, fmt.Errorf("status not 200: %d", res.StatusCode)
}

return res.Body, nil
}

func init() {
bringer.Register("http", HttpBringer)
bringer.Register("https", HttpBringer)
}
Loading

0 comments on commit 484f096

Please sign in to comment.