Skip to content

Commit

Permalink
Merge pull request #34 from 88labs/feat/utf8-bom
Browse files Browse the repository at this point in the history
feat: utf8bom
  • Loading branch information
tomtwinkle authored Oct 7, 2022
2 parents 3c2ebe0 + 7fb62bb commit bf72ada
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/test-utf8bom.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Test UTF-8 BOM

on:
push:
branches:
- main
pull_request:
types:
- opened
- synchronize
- reopened
env:
testdir : ./utf8bom

jobs:
test:
strategy:
matrix:
go-version: [ 1.19.x ]
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}
timeout-minutes: 5
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v3

- name: Go Module Download
working-directory: ${{ env.testdir }}
run: |
go install gotest.tools/gotestsum@latest
go mod download
- name: Test
working-directory: ${{ env.testdir }}
timeout-minutes: 3
run: |
# shellcheck disable=SC2046
gotestsum --junitfile unit-tests.xml -- -v ./... -race -coverprofile="coverage.txt" -covermode=atomic -coverpkg=./...
- uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ${{ env.testdir }}/coverage.txt
14 changes: 14 additions & 0 deletions utf8bom/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/88labs/go-utils/utf8bom

go 1.19

require (
github.com/bxcodec/faker/v3 v3.8.0
github.com/stretchr/testify v1.8.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions utf8bom/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/bxcodec/faker/v3 v3.8.0 h1:F59Qqnsh0BOtZRC+c4cXoB/VNYDMS3R5mlSpxIap1oU=
github.com/bxcodec/faker/v3 v3.8.0/go.mod h1:gF31YgnMSMKgkvl+fyEo1xuSMbEuieyqfeslGYFjneM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
19 changes: 19 additions & 0 deletions utf8bom/utf8bom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utf8bom

var (
BOM = []byte{0xEF, 0xBB, 0xBF}
)

func AddBOM(in []byte) []byte {
return append(BOM, in...)
}

func RemoveBOM(in []byte) []byte {
if len(in) < 3 {
return in
}
if in[0] == BOM[0] && in[1] == BOM[1] && in[2] == BOM[2] {
return in[len(BOM):]
}
return in
}
38 changes: 38 additions & 0 deletions utf8bom/utf8bom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package utf8bom_test

import (
"bytes"
"testing"

"github.com/bxcodec/faker/v3"
"github.com/stretchr/testify/assert"

"github.com/88labs/go-utils/utf8bom"
)

func TestAddBOM(t *testing.T) {
fixtures := bytes.NewBufferString(faker.Name())
withBom := utf8bom.AddBOM(fixtures.Bytes())
assert.Equal(t, len(fixtures.Bytes())+len(utf8bom.BOM), len(withBom))
assert.Equal(t, utf8bom.BOM, withBom[:len(utf8bom.BOM)])
}

func TestRemoveBOM(t *testing.T) {
t.Run("with bom", func(t *testing.T) {
fixtures := bytes.NewBufferString(faker.Name())
withBom := utf8bom.AddBOM(fixtures.Bytes())

removeBom := utf8bom.RemoveBOM(withBom)
assert.Equal(t, fixtures.Bytes(), removeBom)
})
t.Run("not with bom", func(t *testing.T) {
fixtures := bytes.NewBufferString(faker.Name())
removeBom := utf8bom.RemoveBOM(fixtures.Bytes())
assert.Equal(t, fixtures.Bytes(), removeBom)
})
t.Run("1 byte", func(t *testing.T) {
fixtures := bytes.NewBufferString("a")
removeBom := utf8bom.RemoveBOM(fixtures.Bytes())
assert.Equal(t, fixtures.Bytes(), removeBom)
})
}

0 comments on commit bf72ada

Please sign in to comment.