Skip to content

Commit

Permalink
Fix eng content tests; run tests via GitHub Actions (#262)
Browse files Browse the repository at this point in the history
* Add GitHub Actions workflow that runs tests on a PR

* Remove all _content/tour/*/content_test.go files except eng

* Move _content/tour/eng/content_test.go to the root

Fix package name: content -> website.
Walk over _content/tour instead of .

* Replace deprecated io/ioutil with os

* Fix test by replacing // +build with //go:build

* Rename TestContent to TestContentTour

* Fix tests in eng

* Do not run maps/example8.go

* Do not run channels/example10.go

* Do not run context/answer1.go

* Do not run context/example5.go

* Run only eng tests

* Revert localization changes
  • Loading branch information
alexandear authored Jun 27, 2024
1 parent de48a52 commit bcbcc14
Show file tree
Hide file tree
Showing 39 changed files with 74 additions and 61 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Continuous Integration
on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: 1.22

- name: Run tests
run: go test -v ./...
2 changes: 2 additions & 0 deletions _content/tour/eng/algorithms/fun/freq_concurrent.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build OMIT

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0

Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/arrays/example2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/channels/example10.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/composition/assertions/example1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/composition/decoupling/exercise1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/composition/grouping/example1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/composition/mocking/example1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/constants/example4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/context/answer1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/context/example5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/context/exercise1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/data_race/example5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/data_race/example6.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/error-handling/example4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/error-handling/example5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/error-handling/exercise1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/error-handling/exercise2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/exporting/answer1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/exporting/example1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/exporting/example2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/exporting/example3.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/exporting/example4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/exporting/example5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/exporting/exercise1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/generics/type-constraints/exercise1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/interfaces/example2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/interfaces/example3.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/interfaces/example4.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/interfaces/example6.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/maps/example3.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/maps/example6.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/maps/example8.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/methods/example5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/methods/exercise1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || nobuild

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/slices/example1.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 1 addition & 1 deletion _content/tour/eng/slices/example8.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build OMIT
//go:build OMIT || norun

// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
Expand Down
2 changes: 2 additions & 0 deletions _content/tour/eng/welcome/sandbox.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build OMIT

package main

import (
Expand Down
42 changes: 16 additions & 26 deletions _content/tour/eng/content_test.go → content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,38 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package content
package website

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

// Keep github.com/ardanlabs/gotour/external/tour/wc in our go.mod require list for use during test.
_ "github.com/ardanlabs/gotour/external/tour/wc"
)

// Test that all the .go files inside the content file build
// and execute (without checking for output correctness).
// Files that contain the string "// +build no-build" are not built.
// Files that contain the string "// +build no-run" are not executed.
func TestContent(t *testing.T) {
// Files that contain the build constraint "nobuild" are not built.
// Files that contain the build constraint "norun" are not executed.
func TestContentEngTour(t *testing.T) {
if _, err := exec.LookPath("go"); err != nil {
t.Skipf("skipping because 'go' executable not available: %v", err)
}

scratch, err := ioutil.TempDir("", "tour-content-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(scratch)

err = filepath.Walk(".", func(path string, fi os.FileInfo, err error) error {
if filepath.Ext(path) != ".go" {
return nil
err := filepath.Walk(filepath.Join("_content", "tour", "eng"), func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Base(path) == "content_test.go" {
if filepath.Ext(path) != ".go" {
return nil
}
t.Run(path, func(t *testing.T) {
t.Parallel()
if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil {
if err := testSnippet(filepath.ToSlash(path), t.TempDir()); err != nil {
t.Errorf("%v: %v", path, err)
}
})
Expand All @@ -54,21 +44,21 @@ func TestContent(t *testing.T) {
}
}

func testSnippet(t *testing.T, path, scratch string) error {
b, err := ioutil.ReadFile(path)
func testSnippet(path, scratch string) error {
b, err := os.ReadFile(path)
if err != nil {
return err
}

build := string(bytes.SplitN(b, []byte{'\n'}, 2)[0])
if !strings.HasPrefix(build, "// +build ") {
return errors.New("first line is not a +build comment")
if !strings.HasPrefix(build, "//go:build ") {
return errors.New("first line is not a go:build comment")
}
if !strings.Contains(build, "OMIT") {
return errors.New(`+build comment does not contain "OMIT"`)
return errors.New(`go:build comment does not contain "OMIT"`)
}

if strings.Contains(build, "no-build") {
if strings.Contains(build, "nobuild") {
return nil
}
bin := filepath.Join(scratch, filepath.Base(path)+".exe")
Expand All @@ -78,7 +68,7 @@ func testSnippet(t *testing.T, path, scratch string) error {
}
defer os.Remove(bin)

if strings.Contains(build, "no-run") {
if strings.Contains(build, "norun") {
return nil
}
out, err = exec.Command(bin).CombinedOutput()
Expand Down

0 comments on commit bcbcc14

Please sign in to comment.