Skip to content

Commit

Permalink
more maintenance chores
Browse files Browse the repository at this point in the history
  • Loading branch information
thediveo committed Mar 22, 2023
1 parent fb7e6c4 commit 94348e6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![GitHub](https://img.shields.io/github/license/thediveo/spaserve)
![build and test](https://github.com/TheDiveO/spaserve/workflows/build%20and%20test/badge.svg?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/thediveo/spaserve)](https://goreportcard.com/report/github.com/thediveo/spaserve)
![Coverage](https://img.shields.io/badge/Coverage-95.5%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-95.8%25-brightgreen)

`spaserve` serves "Single Page Applications" (SPAs) from Go that are using...

Expand Down
21 changes: 20 additions & 1 deletion scripts/cov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,26 @@ if ! command -v gobadge &>/dev/null; then
fi
fi

go test -covermode=atomic -coverprofile=coverage.out -v -p=1 -count=1 -race ./...
# As of Go 1.20 (and later) we now use the new coverage profiling support that
# also *cough* covers integration tests (even if this particular project might
# not use the latter). The benefit of this slightly more involved approach is
# that we don't need external coverage profile file processing tools anymore,
# but can achieve our goal with just using the standard Go toolchain.

# First, we set up a temporary directory to receive the coverage (binary)
# files...
GOCOVERTMPDIR="$(mktemp -d)"
trap 'rm -rf -- "$GOCOVERTMPDIR"' EXIT
# Now run the (unit) tests with coverage, but don't use the existing textual
# format and instead tell "go test" to produce the new binary coverage data file
# format. This way we can easily run multiple coverage (integration) tests, as
# needed, without worrying about how to aggregate the coverage data later. The
# new Go toolchain already does this for us.
go test -cover -v -p=1 -count=1 -race ./... -args -test.gocoverdir="$GOCOVERTMPDIR"
# Finally transform the coverage information collected in potentially multiple
# runs into the well-proven textual format so we can process it as we have come
# to learn and love.
go tool covdata textfmt -i="$GOCOVERTMPDIR" -o=coverage.out
go tool cover -html=coverage.out -o=coverage.html
go tool cover -func=coverage.out -o=coverage.out
gobadge -filename=coverage.out -green=80 -yellow=50
27 changes: 27 additions & 0 deletions test/httptest/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2023 Harald Albrecht.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package httptest

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestSPAServe(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "spaserve/test/httptest package")
}
46 changes: 46 additions & 0 deletions test/httptest/recorder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2023 Harald Albrecht.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package httptest

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("wrapped HTTP response recorder", Ordered, func() {

var msg string

BeforeEach(func() {
RegisterFailHandler(func(message string, callerSkip ...int) {
msg = message
RegisterFailHandler(Fail) // reset
})
})

It("passes single use of WriteHeader", func() {
rr := NewRecorder()
rr.WriteHeader(200)
Expect(msg).To(BeEmpty())
})

It("fails on superfluous WriteHeader call", func() {
rr := NewRecorder()
rr.WriteHeader(200)
rr.WriteHeader(666)
Expect(msg).To(ContainSubstring("superfluous response.WriteHeader call"))
})

})

0 comments on commit 94348e6

Please sign in to comment.