From 8f5c16e327a49acc513a049395fbefc7057cbd0d Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Sat, 15 Jun 2024 18:38:25 +0300 Subject: [PATCH 01/13] Add GitHub Actions workflow that runs tests on a PR --- .github/workflows/pr.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .github/workflows/pr.yml diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 00000000..b4568988 --- /dev/null +++ b/.github/workflows/pr.yml @@ -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 ./... From 8434897cbccb504886f141d23923b2864f509702 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Sat, 15 Jun 2024 18:40:47 +0300 Subject: [PATCH 02/13] Remove all _content/tour/*/content_test.go files except eng --- _content/tour/fre/content_test.go | 89 ------------------------------- _content/tour/ger/content_test.go | 89 ------------------------------- _content/tour/grc/content_test.go | 89 ------------------------------- _content/tour/ita/content_test.go | 89 ------------------------------- _content/tour/per/content_test.go | 89 ------------------------------- _content/tour/pol/content_test.go | 89 ------------------------------- _content/tour/por/content_test.go | 89 ------------------------------- _content/tour/rus/content_test.go | 89 ------------------------------- _content/tour/tur/content_test.go | 89 ------------------------------- 9 files changed, 801 deletions(-) delete mode 100644 _content/tour/fre/content_test.go delete mode 100644 _content/tour/ger/content_test.go delete mode 100644 _content/tour/grc/content_test.go delete mode 100644 _content/tour/ita/content_test.go delete mode 100644 _content/tour/per/content_test.go delete mode 100644 _content/tour/pol/content_test.go delete mode 100644 _content/tour/por/content_test.go delete mode 100644 _content/tour/rus/content_test.go delete mode 100644 _content/tour/tur/content_test.go diff --git a/_content/tour/fre/content_test.go b/_content/tour/fre/content_test.go deleted file mode 100644 index 6c7b22d4..00000000 --- a/_content/tour/fre/content_test.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package content - -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) { - 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 - } - if filepath.Base(path) == "content_test.go" { - return nil - } - t.Run(path, func(t *testing.T) { - t.Parallel() - if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { - t.Errorf("%v: %v", path, err) - } - }) - return nil - }) - if err != nil { - t.Error(err) - } -} - -func testSnippet(t *testing.T, path, scratch string) error { - b, err := ioutil.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.Contains(build, "OMIT") { - return errors.New(`+build comment does not contain "OMIT"`) - } - - if strings.Contains(build, "no-build") { - return nil - } - bin := filepath.Join(scratch, filepath.Base(path)+".exe") - out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() - if err != nil { - return fmt.Errorf("build error: %v\noutput:\n%s", err, out) - } - defer os.Remove(bin) - - if strings.Contains(build, "no-run") { - return nil - } - out, err = exec.Command(bin).CombinedOutput() - if err != nil { - return fmt.Errorf("%v\nOutput:\n%s", err, out) - } - return nil -} diff --git a/_content/tour/ger/content_test.go b/_content/tour/ger/content_test.go deleted file mode 100644 index 6c7b22d4..00000000 --- a/_content/tour/ger/content_test.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package content - -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) { - 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 - } - if filepath.Base(path) == "content_test.go" { - return nil - } - t.Run(path, func(t *testing.T) { - t.Parallel() - if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { - t.Errorf("%v: %v", path, err) - } - }) - return nil - }) - if err != nil { - t.Error(err) - } -} - -func testSnippet(t *testing.T, path, scratch string) error { - b, err := ioutil.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.Contains(build, "OMIT") { - return errors.New(`+build comment does not contain "OMIT"`) - } - - if strings.Contains(build, "no-build") { - return nil - } - bin := filepath.Join(scratch, filepath.Base(path)+".exe") - out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() - if err != nil { - return fmt.Errorf("build error: %v\noutput:\n%s", err, out) - } - defer os.Remove(bin) - - if strings.Contains(build, "no-run") { - return nil - } - out, err = exec.Command(bin).CombinedOutput() - if err != nil { - return fmt.Errorf("%v\nOutput:\n%s", err, out) - } - return nil -} diff --git a/_content/tour/grc/content_test.go b/_content/tour/grc/content_test.go deleted file mode 100644 index 6c7b22d4..00000000 --- a/_content/tour/grc/content_test.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package content - -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) { - 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 - } - if filepath.Base(path) == "content_test.go" { - return nil - } - t.Run(path, func(t *testing.T) { - t.Parallel() - if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { - t.Errorf("%v: %v", path, err) - } - }) - return nil - }) - if err != nil { - t.Error(err) - } -} - -func testSnippet(t *testing.T, path, scratch string) error { - b, err := ioutil.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.Contains(build, "OMIT") { - return errors.New(`+build comment does not contain "OMIT"`) - } - - if strings.Contains(build, "no-build") { - return nil - } - bin := filepath.Join(scratch, filepath.Base(path)+".exe") - out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() - if err != nil { - return fmt.Errorf("build error: %v\noutput:\n%s", err, out) - } - defer os.Remove(bin) - - if strings.Contains(build, "no-run") { - return nil - } - out, err = exec.Command(bin).CombinedOutput() - if err != nil { - return fmt.Errorf("%v\nOutput:\n%s", err, out) - } - return nil -} diff --git a/_content/tour/ita/content_test.go b/_content/tour/ita/content_test.go deleted file mode 100644 index 6c7b22d4..00000000 --- a/_content/tour/ita/content_test.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package content - -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) { - 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 - } - if filepath.Base(path) == "content_test.go" { - return nil - } - t.Run(path, func(t *testing.T) { - t.Parallel() - if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { - t.Errorf("%v: %v", path, err) - } - }) - return nil - }) - if err != nil { - t.Error(err) - } -} - -func testSnippet(t *testing.T, path, scratch string) error { - b, err := ioutil.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.Contains(build, "OMIT") { - return errors.New(`+build comment does not contain "OMIT"`) - } - - if strings.Contains(build, "no-build") { - return nil - } - bin := filepath.Join(scratch, filepath.Base(path)+".exe") - out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() - if err != nil { - return fmt.Errorf("build error: %v\noutput:\n%s", err, out) - } - defer os.Remove(bin) - - if strings.Contains(build, "no-run") { - return nil - } - out, err = exec.Command(bin).CombinedOutput() - if err != nil { - return fmt.Errorf("%v\nOutput:\n%s", err, out) - } - return nil -} diff --git a/_content/tour/per/content_test.go b/_content/tour/per/content_test.go deleted file mode 100644 index 6c7b22d4..00000000 --- a/_content/tour/per/content_test.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package content - -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) { - 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 - } - if filepath.Base(path) == "content_test.go" { - return nil - } - t.Run(path, func(t *testing.T) { - t.Parallel() - if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { - t.Errorf("%v: %v", path, err) - } - }) - return nil - }) - if err != nil { - t.Error(err) - } -} - -func testSnippet(t *testing.T, path, scratch string) error { - b, err := ioutil.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.Contains(build, "OMIT") { - return errors.New(`+build comment does not contain "OMIT"`) - } - - if strings.Contains(build, "no-build") { - return nil - } - bin := filepath.Join(scratch, filepath.Base(path)+".exe") - out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() - if err != nil { - return fmt.Errorf("build error: %v\noutput:\n%s", err, out) - } - defer os.Remove(bin) - - if strings.Contains(build, "no-run") { - return nil - } - out, err = exec.Command(bin).CombinedOutput() - if err != nil { - return fmt.Errorf("%v\nOutput:\n%s", err, out) - } - return nil -} diff --git a/_content/tour/pol/content_test.go b/_content/tour/pol/content_test.go deleted file mode 100644 index 6c7b22d4..00000000 --- a/_content/tour/pol/content_test.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package content - -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) { - 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 - } - if filepath.Base(path) == "content_test.go" { - return nil - } - t.Run(path, func(t *testing.T) { - t.Parallel() - if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { - t.Errorf("%v: %v", path, err) - } - }) - return nil - }) - if err != nil { - t.Error(err) - } -} - -func testSnippet(t *testing.T, path, scratch string) error { - b, err := ioutil.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.Contains(build, "OMIT") { - return errors.New(`+build comment does not contain "OMIT"`) - } - - if strings.Contains(build, "no-build") { - return nil - } - bin := filepath.Join(scratch, filepath.Base(path)+".exe") - out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() - if err != nil { - return fmt.Errorf("build error: %v\noutput:\n%s", err, out) - } - defer os.Remove(bin) - - if strings.Contains(build, "no-run") { - return nil - } - out, err = exec.Command(bin).CombinedOutput() - if err != nil { - return fmt.Errorf("%v\nOutput:\n%s", err, out) - } - return nil -} diff --git a/_content/tour/por/content_test.go b/_content/tour/por/content_test.go deleted file mode 100644 index 6c7b22d4..00000000 --- a/_content/tour/por/content_test.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package content - -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) { - 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 - } - if filepath.Base(path) == "content_test.go" { - return nil - } - t.Run(path, func(t *testing.T) { - t.Parallel() - if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { - t.Errorf("%v: %v", path, err) - } - }) - return nil - }) - if err != nil { - t.Error(err) - } -} - -func testSnippet(t *testing.T, path, scratch string) error { - b, err := ioutil.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.Contains(build, "OMIT") { - return errors.New(`+build comment does not contain "OMIT"`) - } - - if strings.Contains(build, "no-build") { - return nil - } - bin := filepath.Join(scratch, filepath.Base(path)+".exe") - out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() - if err != nil { - return fmt.Errorf("build error: %v\noutput:\n%s", err, out) - } - defer os.Remove(bin) - - if strings.Contains(build, "no-run") { - return nil - } - out, err = exec.Command(bin).CombinedOutput() - if err != nil { - return fmt.Errorf("%v\nOutput:\n%s", err, out) - } - return nil -} diff --git a/_content/tour/rus/content_test.go b/_content/tour/rus/content_test.go deleted file mode 100644 index 6c7b22d4..00000000 --- a/_content/tour/rus/content_test.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package content - -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) { - 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 - } - if filepath.Base(path) == "content_test.go" { - return nil - } - t.Run(path, func(t *testing.T) { - t.Parallel() - if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { - t.Errorf("%v: %v", path, err) - } - }) - return nil - }) - if err != nil { - t.Error(err) - } -} - -func testSnippet(t *testing.T, path, scratch string) error { - b, err := ioutil.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.Contains(build, "OMIT") { - return errors.New(`+build comment does not contain "OMIT"`) - } - - if strings.Contains(build, "no-build") { - return nil - } - bin := filepath.Join(scratch, filepath.Base(path)+".exe") - out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() - if err != nil { - return fmt.Errorf("build error: %v\noutput:\n%s", err, out) - } - defer os.Remove(bin) - - if strings.Contains(build, "no-run") { - return nil - } - out, err = exec.Command(bin).CombinedOutput() - if err != nil { - return fmt.Errorf("%v\nOutput:\n%s", err, out) - } - return nil -} diff --git a/_content/tour/tur/content_test.go b/_content/tour/tur/content_test.go deleted file mode 100644 index 6c7b22d4..00000000 --- a/_content/tour/tur/content_test.go +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package content - -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) { - 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 - } - if filepath.Base(path) == "content_test.go" { - return nil - } - t.Run(path, func(t *testing.T) { - t.Parallel() - if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { - t.Errorf("%v: %v", path, err) - } - }) - return nil - }) - if err != nil { - t.Error(err) - } -} - -func testSnippet(t *testing.T, path, scratch string) error { - b, err := ioutil.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.Contains(build, "OMIT") { - return errors.New(`+build comment does not contain "OMIT"`) - } - - if strings.Contains(build, "no-build") { - return nil - } - bin := filepath.Join(scratch, filepath.Base(path)+".exe") - out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() - if err != nil { - return fmt.Errorf("build error: %v\noutput:\n%s", err, out) - } - defer os.Remove(bin) - - if strings.Contains(build, "no-run") { - return nil - } - out, err = exec.Command(bin).CombinedOutput() - if err != nil { - return fmt.Errorf("%v\nOutput:\n%s", err, out) - } - return nil -} From 280049b5e4b84128c0269dc9522bbbc6f3416f1e Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Sat, 15 Jun 2024 19:07:45 +0300 Subject: [PATCH 03/13] Move _content/tour/eng/content_test.go to the root Fix package name: content -> website. Walk over _content/tour instead of . --- _content/tour/eng/content_test.go => content_test.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) rename _content/tour/eng/content_test.go => content_test.go (89%) diff --git a/_content/tour/eng/content_test.go b/content_test.go similarity index 89% rename from _content/tour/eng/content_test.go rename to content_test.go index 6c7b22d4..3894f049 100644 --- a/_content/tour/eng/content_test.go +++ b/content_test.go @@ -2,7 +2,7 @@ // 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" @@ -14,9 +14,6 @@ import ( "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 @@ -34,7 +31,7 @@ func TestContent(t *testing.T) { } defer os.RemoveAll(scratch) - err = filepath.Walk(".", func(path string, fi os.FileInfo, err error) error { + err = filepath.Walk(filepath.Join("_content", "tour"), func(path string, fi os.FileInfo, err error) error { if filepath.Ext(path) != ".go" { return nil } From c90686b367edf2c5b5ed595338d9ee7d17a3ba4c Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Sat, 15 Jun 2024 19:46:23 +0300 Subject: [PATCH 04/13] Replace deprecated io/ioutil with os --- content_test.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/content_test.go b/content_test.go index 3894f049..46ae9d7c 100644 --- a/content_test.go +++ b/content_test.go @@ -8,7 +8,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -25,13 +24,7 @@ func TestContent(t *testing.T) { 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(filepath.Join("_content", "tour"), func(path string, fi os.FileInfo, err error) error { + err := filepath.Walk(filepath.Join("_content", "tour"), func(path string, fi os.FileInfo, err error) error { if filepath.Ext(path) != ".go" { return nil } @@ -40,7 +33,7 @@ func TestContent(t *testing.T) { } 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) } }) @@ -51,8 +44,8 @@ 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 } From 8b5cff794deea4a6366aacccbc3d8a64d9176b95 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Sat, 15 Jun 2024 19:48:11 +0300 Subject: [PATCH 05/13] Fix test by replacing // +build with //go:build --- content_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/content_test.go b/content_test.go index 46ae9d7c..18f0e847 100644 --- a/content_test.go +++ b/content_test.go @@ -17,8 +17,8 @@ import ( // 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. +// Files that contain the build constraint "nobuild" are not built. +// Files that contain the build constraint "norun" are not executed. func TestContent(t *testing.T) { if _, err := exec.LookPath("go"); err != nil { t.Skipf("skipping because 'go' executable not available: %v", err) @@ -51,14 +51,14 @@ func testSnippet(path, scratch string) error { } 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") @@ -68,7 +68,7 @@ func testSnippet(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() From 59e85f161b8bc4fc15b0efc542ad49762269abed Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Sat, 15 Jun 2024 19:49:38 +0300 Subject: [PATCH 06/13] Rename TestContent to TestContentTour --- content_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content_test.go b/content_test.go index 18f0e847..719c6dae 100644 --- a/content_test.go +++ b/content_test.go @@ -19,7 +19,7 @@ import ( // and execute (without checking for output correctness). // Files that contain the build constraint "nobuild" are not built. // Files that contain the build constraint "norun" are not executed. -func TestContent(t *testing.T) { +func TestContentTour(t *testing.T) { if _, err := exec.LookPath("go"); err != nil { t.Skipf("skipping because 'go' executable not available: %v", err) } From c0b7daa1f6e92fe239f2f8ec838988dd5c72ec1c Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Sat, 15 Jun 2024 19:53:07 +0300 Subject: [PATCH 07/13] Fix tests in eng --- _content/tour/eng/algorithms/fun/freq_concurrent.go | 2 ++ _content/tour/eng/arrays/example2.go | 2 +- _content/tour/eng/composition/assertions/example1.go | 2 +- _content/tour/eng/composition/decoupling/exercise1.go | 2 +- _content/tour/eng/composition/grouping/example1.go | 2 +- _content/tour/eng/composition/mocking/example1.go | 2 +- _content/tour/eng/constants/example4.go | 2 +- _content/tour/eng/context/exercise1.go | 2 +- _content/tour/eng/data_race/example5.go | 2 +- _content/tour/eng/data_race/example6.go | 2 +- _content/tour/eng/error-handling/example4.go | 2 +- _content/tour/eng/error-handling/example5.go | 2 +- _content/tour/eng/error-handling/exercise1.go | 2 +- _content/tour/eng/error-handling/exercise2.go | 2 +- _content/tour/eng/exporting/answer1.go | 2 +- _content/tour/eng/exporting/example1.go | 2 +- _content/tour/eng/exporting/example2.go | 2 +- _content/tour/eng/exporting/example3.go | 2 +- _content/tour/eng/exporting/example4.go | 2 +- _content/tour/eng/exporting/example5.go | 2 +- _content/tour/eng/exporting/exercise1.go | 2 +- _content/tour/eng/generics/type-constraints/exercise1.go | 2 +- _content/tour/eng/interfaces/example2.go | 2 +- _content/tour/eng/interfaces/example3.go | 2 +- _content/tour/eng/interfaces/example4.go | 2 +- _content/tour/eng/interfaces/example6.go | 2 +- _content/tour/eng/maps/example3.go | 2 +- _content/tour/eng/maps/example6.go | 2 +- _content/tour/eng/methods/example5.go | 2 +- _content/tour/eng/methods/exercise1.go | 2 +- _content/tour/eng/slices/example1.go | 2 +- _content/tour/eng/slices/example8.go | 2 +- _content/tour/eng/welcome/sandbox.go | 2 ++ 33 files changed, 35 insertions(+), 31 deletions(-) diff --git a/_content/tour/eng/algorithms/fun/freq_concurrent.go b/_content/tour/eng/algorithms/fun/freq_concurrent.go index 3b91df41..edfa678f 100644 --- a/_content/tour/eng/algorithms/fun/freq_concurrent.go +++ b/_content/tour/eng/algorithms/fun/freq_concurrent.go @@ -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 diff --git a/_content/tour/eng/arrays/example2.go b/_content/tour/eng/arrays/example2.go index ce6e4299..5c41c454 100644 --- a/_content/tour/eng/arrays/example2.go +++ b/_content/tour/eng/arrays/example2.go @@ -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 diff --git a/_content/tour/eng/composition/assertions/example1.go b/_content/tour/eng/composition/assertions/example1.go index 5d1191cb..f0e81147 100644 --- a/_content/tour/eng/composition/assertions/example1.go +++ b/_content/tour/eng/composition/assertions/example1.go @@ -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 diff --git a/_content/tour/eng/composition/decoupling/exercise1.go b/_content/tour/eng/composition/decoupling/exercise1.go index db2f3a41..98bd38df 100644 --- a/_content/tour/eng/composition/decoupling/exercise1.go +++ b/_content/tour/eng/composition/decoupling/exercise1.go @@ -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 diff --git a/_content/tour/eng/composition/grouping/example1.go b/_content/tour/eng/composition/grouping/example1.go index 474b91ca..59d0bea8 100644 --- a/_content/tour/eng/composition/grouping/example1.go +++ b/_content/tour/eng/composition/grouping/example1.go @@ -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 diff --git a/_content/tour/eng/composition/mocking/example1.go b/_content/tour/eng/composition/mocking/example1.go index e72930c1..445b6e18 100644 --- a/_content/tour/eng/composition/mocking/example1.go +++ b/_content/tour/eng/composition/mocking/example1.go @@ -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 diff --git a/_content/tour/eng/constants/example4.go b/_content/tour/eng/constants/example4.go index 1b735f9c..64560df1 100644 --- a/_content/tour/eng/constants/example4.go +++ b/_content/tour/eng/constants/example4.go @@ -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 diff --git a/_content/tour/eng/context/exercise1.go b/_content/tour/eng/context/exercise1.go index d7225c6c..cf7d6224 100644 --- a/_content/tour/eng/context/exercise1.go +++ b/_content/tour/eng/context/exercise1.go @@ -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 diff --git a/_content/tour/eng/data_race/example5.go b/_content/tour/eng/data_race/example5.go index dd7cb0fc..fda16cd5 100644 --- a/_content/tour/eng/data_race/example5.go +++ b/_content/tour/eng/data_race/example5.go @@ -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 diff --git a/_content/tour/eng/data_race/example6.go b/_content/tour/eng/data_race/example6.go index 61243b57..8ab7578d 100644 --- a/_content/tour/eng/data_race/example6.go +++ b/_content/tour/eng/data_race/example6.go @@ -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 diff --git a/_content/tour/eng/error-handling/example4.go b/_content/tour/eng/error-handling/example4.go index 40f8303e..5e2e7f48 100644 --- a/_content/tour/eng/error-handling/example4.go +++ b/_content/tour/eng/error-handling/example4.go @@ -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 diff --git a/_content/tour/eng/error-handling/example5.go b/_content/tour/eng/error-handling/example5.go index de920b11..4c677b32 100644 --- a/_content/tour/eng/error-handling/example5.go +++ b/_content/tour/eng/error-handling/example5.go @@ -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 diff --git a/_content/tour/eng/error-handling/exercise1.go b/_content/tour/eng/error-handling/exercise1.go index 0fb21e45..378257d8 100644 --- a/_content/tour/eng/error-handling/exercise1.go +++ b/_content/tour/eng/error-handling/exercise1.go @@ -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 diff --git a/_content/tour/eng/error-handling/exercise2.go b/_content/tour/eng/error-handling/exercise2.go index 59310aa9..e2883ef2 100644 --- a/_content/tour/eng/error-handling/exercise2.go +++ b/_content/tour/eng/error-handling/exercise2.go @@ -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 diff --git a/_content/tour/eng/exporting/answer1.go b/_content/tour/eng/exporting/answer1.go index f0abc4c6..dcd28e17 100644 --- a/_content/tour/eng/exporting/answer1.go +++ b/_content/tour/eng/exporting/answer1.go @@ -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 diff --git a/_content/tour/eng/exporting/example1.go b/_content/tour/eng/exporting/example1.go index 5cd1820c..a828b2f6 100644 --- a/_content/tour/eng/exporting/example1.go +++ b/_content/tour/eng/exporting/example1.go @@ -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 diff --git a/_content/tour/eng/exporting/example2.go b/_content/tour/eng/exporting/example2.go index cec2201e..91735097 100644 --- a/_content/tour/eng/exporting/example2.go +++ b/_content/tour/eng/exporting/example2.go @@ -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 diff --git a/_content/tour/eng/exporting/example3.go b/_content/tour/eng/exporting/example3.go index 00a1dda7..1ba82103 100644 --- a/_content/tour/eng/exporting/example3.go +++ b/_content/tour/eng/exporting/example3.go @@ -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 diff --git a/_content/tour/eng/exporting/example4.go b/_content/tour/eng/exporting/example4.go index f2b0174a..93d06902 100644 --- a/_content/tour/eng/exporting/example4.go +++ b/_content/tour/eng/exporting/example4.go @@ -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 diff --git a/_content/tour/eng/exporting/example5.go b/_content/tour/eng/exporting/example5.go index 70da23ed..8ae0fa09 100644 --- a/_content/tour/eng/exporting/example5.go +++ b/_content/tour/eng/exporting/example5.go @@ -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 diff --git a/_content/tour/eng/exporting/exercise1.go b/_content/tour/eng/exporting/exercise1.go index bafbd5a5..4ff23346 100644 --- a/_content/tour/eng/exporting/exercise1.go +++ b/_content/tour/eng/exporting/exercise1.go @@ -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 diff --git a/_content/tour/eng/generics/type-constraints/exercise1.go b/_content/tour/eng/generics/type-constraints/exercise1.go index 01dd1324..13c74e40 100644 --- a/_content/tour/eng/generics/type-constraints/exercise1.go +++ b/_content/tour/eng/generics/type-constraints/exercise1.go @@ -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 diff --git a/_content/tour/eng/interfaces/example2.go b/_content/tour/eng/interfaces/example2.go index a9e10aa4..3b56f6fd 100644 --- a/_content/tour/eng/interfaces/example2.go +++ b/_content/tour/eng/interfaces/example2.go @@ -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 diff --git a/_content/tour/eng/interfaces/example3.go b/_content/tour/eng/interfaces/example3.go index 2ed6c756..cbd2a139 100644 --- a/_content/tour/eng/interfaces/example3.go +++ b/_content/tour/eng/interfaces/example3.go @@ -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 diff --git a/_content/tour/eng/interfaces/example4.go b/_content/tour/eng/interfaces/example4.go index acb8e2f6..e8cab8ba 100644 --- a/_content/tour/eng/interfaces/example4.go +++ b/_content/tour/eng/interfaces/example4.go @@ -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 diff --git a/_content/tour/eng/interfaces/example6.go b/_content/tour/eng/interfaces/example6.go index 68825182..5187153e 100644 --- a/_content/tour/eng/interfaces/example6.go +++ b/_content/tour/eng/interfaces/example6.go @@ -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 diff --git a/_content/tour/eng/maps/example3.go b/_content/tour/eng/maps/example3.go index a1fb2fa6..a9913573 100644 --- a/_content/tour/eng/maps/example3.go +++ b/_content/tour/eng/maps/example3.go @@ -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 diff --git a/_content/tour/eng/maps/example6.go b/_content/tour/eng/maps/example6.go index bfffd74e..e79d9f1a 100644 --- a/_content/tour/eng/maps/example6.go +++ b/_content/tour/eng/maps/example6.go @@ -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 diff --git a/_content/tour/eng/methods/example5.go b/_content/tour/eng/methods/example5.go index fb0d8cf7..13275785 100644 --- a/_content/tour/eng/methods/example5.go +++ b/_content/tour/eng/methods/example5.go @@ -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 diff --git a/_content/tour/eng/methods/exercise1.go b/_content/tour/eng/methods/exercise1.go index 555d0ef1..8e09f37c 100644 --- a/_content/tour/eng/methods/exercise1.go +++ b/_content/tour/eng/methods/exercise1.go @@ -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 diff --git a/_content/tour/eng/slices/example1.go b/_content/tour/eng/slices/example1.go index 36f418d4..f86c3764 100644 --- a/_content/tour/eng/slices/example1.go +++ b/_content/tour/eng/slices/example1.go @@ -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 diff --git a/_content/tour/eng/slices/example8.go b/_content/tour/eng/slices/example8.go index af4361da..ad9b748d 100644 --- a/_content/tour/eng/slices/example8.go +++ b/_content/tour/eng/slices/example8.go @@ -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 diff --git a/_content/tour/eng/welcome/sandbox.go b/_content/tour/eng/welcome/sandbox.go index 183bb929..9525b562 100644 --- a/_content/tour/eng/welcome/sandbox.go +++ b/_content/tour/eng/welcome/sandbox.go @@ -1,3 +1,5 @@ +//go:build OMIT + package main import ( From 05f63ef2aeedccbe05ea0270de5f836d6c5f6441 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Tue, 25 Jun 2024 17:04:38 +0300 Subject: [PATCH 08/13] Do not run maps/example8.go --- _content/tour/eng/maps/example8.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_content/tour/eng/maps/example8.go b/_content/tour/eng/maps/example8.go index 359f752a..0d72d9e7 100644 --- a/_content/tour/eng/maps/example8.go +++ b/_content/tour/eng/maps/example8.go @@ -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 From 7cab71e305b810b2a272696515a575bf49052504 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Tue, 25 Jun 2024 17:08:15 +0300 Subject: [PATCH 09/13] Do not run channels/example10.go --- _content/tour/eng/channels/example10.go | 2 +- _content/tour/fre/channels/example10.go | 2 +- _content/tour/ger/channels/example10.go | 2 +- _content/tour/grc/channels/example10.go | 2 +- _content/tour/ita/channels/example10.go | 2 +- _content/tour/per/channels/example10.go | 2 +- _content/tour/pol/channels/example10.go | 2 +- _content/tour/por/channels/example10.go | 2 +- _content/tour/rus/channels/example10.go | 2 +- _content/tour/tur/channels/example10.go | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/_content/tour/eng/channels/example10.go b/_content/tour/eng/channels/example10.go index e680c74a..ed01faa0 100644 --- a/_content/tour/eng/channels/example10.go +++ b/_content/tour/eng/channels/example10.go @@ -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 diff --git a/_content/tour/fre/channels/example10.go b/_content/tour/fre/channels/example10.go index e680c74a..ed01faa0 100644 --- a/_content/tour/fre/channels/example10.go +++ b/_content/tour/fre/channels/example10.go @@ -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 diff --git a/_content/tour/ger/channels/example10.go b/_content/tour/ger/channels/example10.go index e680c74a..ed01faa0 100644 --- a/_content/tour/ger/channels/example10.go +++ b/_content/tour/ger/channels/example10.go @@ -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 diff --git a/_content/tour/grc/channels/example10.go b/_content/tour/grc/channels/example10.go index 9155ba82..e7c6e22d 100644 --- a/_content/tour/grc/channels/example10.go +++ b/_content/tour/grc/channels/example10.go @@ -1,4 +1,4 @@ -//go:build OMIT +//go:build OMIT || norun // Όλα τα υλικά είναι αδειοδοτημένα υπό την Άδεια Apache Έκδοση 2.0, Ιανουάριος 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/ita/channels/example10.go b/_content/tour/ita/channels/example10.go index e680c74a..ed01faa0 100644 --- a/_content/tour/ita/channels/example10.go +++ b/_content/tour/ita/channels/example10.go @@ -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 diff --git a/_content/tour/per/channels/example10.go b/_content/tour/per/channels/example10.go index e680c74a..ed01faa0 100644 --- a/_content/tour/per/channels/example10.go +++ b/_content/tour/per/channels/example10.go @@ -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 diff --git a/_content/tour/pol/channels/example10.go b/_content/tour/pol/channels/example10.go index e680c74a..ed01faa0 100644 --- a/_content/tour/pol/channels/example10.go +++ b/_content/tour/pol/channels/example10.go @@ -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 diff --git a/_content/tour/por/channels/example10.go b/_content/tour/por/channels/example10.go index e680c74a..ed01faa0 100644 --- a/_content/tour/por/channels/example10.go +++ b/_content/tour/por/channels/example10.go @@ -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 diff --git a/_content/tour/rus/channels/example10.go b/_content/tour/rus/channels/example10.go index e680c74a..ed01faa0 100644 --- a/_content/tour/rus/channels/example10.go +++ b/_content/tour/rus/channels/example10.go @@ -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 diff --git a/_content/tour/tur/channels/example10.go b/_content/tour/tur/channels/example10.go index e680c74a..ed01faa0 100644 --- a/_content/tour/tur/channels/example10.go +++ b/_content/tour/tur/channels/example10.go @@ -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 From c42f63f10e798f8982819f68158648598f9aebd8 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Tue, 25 Jun 2024 17:08:43 +0300 Subject: [PATCH 10/13] Do not run context/answer1.go --- _content/tour/eng/context/answer1.go | 2 +- _content/tour/fre/context/answer1.go | 2 +- _content/tour/ger/context/answer1.go | 2 +- _content/tour/grc/context/answer1.go | 2 +- _content/tour/ita/context/answer1.go | 2 +- _content/tour/per/context/answer1.go | 2 +- _content/tour/pol/context/answer1.go | 2 +- _content/tour/por/context/answer1.go | 2 +- _content/tour/rus/context/answer1.go | 2 +- _content/tour/tur/context/answer1.go | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/_content/tour/eng/context/answer1.go b/_content/tour/eng/context/answer1.go index 8291bcf3..49fca71b 100644 --- a/_content/tour/eng/context/answer1.go +++ b/_content/tour/eng/context/answer1.go @@ -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 diff --git a/_content/tour/fre/context/answer1.go b/_content/tour/fre/context/answer1.go index 8291bcf3..49fca71b 100644 --- a/_content/tour/fre/context/answer1.go +++ b/_content/tour/fre/context/answer1.go @@ -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 diff --git a/_content/tour/ger/context/answer1.go b/_content/tour/ger/context/answer1.go index 8291bcf3..b6febd74 100644 --- a/_content/tour/ger/context/answer1.go +++ b/_content/tour/ger/context/answer1.go @@ -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 diff --git a/_content/tour/grc/context/answer1.go b/_content/tour/grc/context/answer1.go index aa4f11c5..a2dd4147 100644 --- a/_content/tour/grc/context/answer1.go +++ b/_content/tour/grc/context/answer1.go @@ -1,4 +1,4 @@ -//go:build OMIT +//go:build OMIT || norun // Όλα τα υλικά είναι αδειοδοτημένα υπό την Άδεια Apache Έκδοση 2.0, Ιανουάριος 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/ita/context/answer1.go b/_content/tour/ita/context/answer1.go index 8291bcf3..49fca71b 100644 --- a/_content/tour/ita/context/answer1.go +++ b/_content/tour/ita/context/answer1.go @@ -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 diff --git a/_content/tour/per/context/answer1.go b/_content/tour/per/context/answer1.go index 8291bcf3..49fca71b 100644 --- a/_content/tour/per/context/answer1.go +++ b/_content/tour/per/context/answer1.go @@ -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 diff --git a/_content/tour/pol/context/answer1.go b/_content/tour/pol/context/answer1.go index 8291bcf3..49fca71b 100644 --- a/_content/tour/pol/context/answer1.go +++ b/_content/tour/pol/context/answer1.go @@ -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 diff --git a/_content/tour/por/context/answer1.go b/_content/tour/por/context/answer1.go index 8291bcf3..49fca71b 100644 --- a/_content/tour/por/context/answer1.go +++ b/_content/tour/por/context/answer1.go @@ -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 diff --git a/_content/tour/rus/context/answer1.go b/_content/tour/rus/context/answer1.go index 8291bcf3..49fca71b 100644 --- a/_content/tour/rus/context/answer1.go +++ b/_content/tour/rus/context/answer1.go @@ -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 diff --git a/_content/tour/tur/context/answer1.go b/_content/tour/tur/context/answer1.go index 8291bcf3..49fca71b 100644 --- a/_content/tour/tur/context/answer1.go +++ b/_content/tour/tur/context/answer1.go @@ -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 From d1b936c4ecb258cf25796880b294fcf4f2e46933 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Tue, 25 Jun 2024 17:09:50 +0300 Subject: [PATCH 11/13] Do not run context/example5.go --- _content/tour/eng/context/example5.go | 2 +- _content/tour/fre/context/example5.go | 2 +- _content/tour/ger/context/example5.go | 2 +- _content/tour/grc/context/example5.go | 2 +- _content/tour/ita/context/example5.go | 2 +- _content/tour/per/context/example5.go | 2 +- _content/tour/pol/context/example5.go | 2 +- _content/tour/por/context/example5.go | 2 +- _content/tour/rus/context/example5.go | 2 +- _content/tour/tur/context/example5.go | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/_content/tour/eng/context/example5.go b/_content/tour/eng/context/example5.go index cf819766..a04854fa 100644 --- a/_content/tour/eng/context/example5.go +++ b/_content/tour/eng/context/example5.go @@ -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 diff --git a/_content/tour/fre/context/example5.go b/_content/tour/fre/context/example5.go index cf819766..a04854fa 100644 --- a/_content/tour/fre/context/example5.go +++ b/_content/tour/fre/context/example5.go @@ -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 diff --git a/_content/tour/ger/context/example5.go b/_content/tour/ger/context/example5.go index cf819766..a04854fa 100644 --- a/_content/tour/ger/context/example5.go +++ b/_content/tour/ger/context/example5.go @@ -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 diff --git a/_content/tour/grc/context/example5.go b/_content/tour/grc/context/example5.go index fce0c6ed..1cbcdb05 100644 --- a/_content/tour/grc/context/example5.go +++ b/_content/tour/grc/context/example5.go @@ -1,4 +1,4 @@ -//go:build OMIT +//go:build OMIT || norun // Όλα τα υλικά είναι αδειοδοτημένα υπό την Άδεια Apache Έκδοση 2.0, Ιανουάριος 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/ita/context/example5.go b/_content/tour/ita/context/example5.go index cf819766..a04854fa 100644 --- a/_content/tour/ita/context/example5.go +++ b/_content/tour/ita/context/example5.go @@ -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 diff --git a/_content/tour/per/context/example5.go b/_content/tour/per/context/example5.go index cf819766..a04854fa 100644 --- a/_content/tour/per/context/example5.go +++ b/_content/tour/per/context/example5.go @@ -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 diff --git a/_content/tour/pol/context/example5.go b/_content/tour/pol/context/example5.go index cf819766..a04854fa 100644 --- a/_content/tour/pol/context/example5.go +++ b/_content/tour/pol/context/example5.go @@ -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 diff --git a/_content/tour/por/context/example5.go b/_content/tour/por/context/example5.go index cf819766..a04854fa 100644 --- a/_content/tour/por/context/example5.go +++ b/_content/tour/por/context/example5.go @@ -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 diff --git a/_content/tour/rus/context/example5.go b/_content/tour/rus/context/example5.go index cf819766..a04854fa 100644 --- a/_content/tour/rus/context/example5.go +++ b/_content/tour/rus/context/example5.go @@ -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 diff --git a/_content/tour/tur/context/example5.go b/_content/tour/tur/context/example5.go index cf819766..a04854fa 100644 --- a/_content/tour/tur/context/example5.go +++ b/_content/tour/tur/context/example5.go @@ -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 From 2be6adc25ead983636cb35faf1a0aa43f3922131 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Tue, 25 Jun 2024 17:10:05 +0300 Subject: [PATCH 12/13] Run only eng tests --- content_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content_test.go b/content_test.go index 719c6dae..a1819a2c 100644 --- a/content_test.go +++ b/content_test.go @@ -19,16 +19,16 @@ import ( // and execute (without checking for output correctness). // Files that contain the build constraint "nobuild" are not built. // Files that contain the build constraint "norun" are not executed. -func TestContentTour(t *testing.T) { +func TestContentEngTour(t *testing.T) { if _, err := exec.LookPath("go"); err != nil { t.Skipf("skipping because 'go' executable not available: %v", err) } - err := filepath.Walk(filepath.Join("_content", "tour"), 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) { From 2946c4e2df33d7acf1b3c498c6499c8fbad51598 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Wed, 26 Jun 2024 17:30:43 +0300 Subject: [PATCH 13/13] Revert localization changes --- _content/tour/fre/channels/example10.go | 2 +- _content/tour/fre/content_test.go | 89 +++++++++++++++++++++++++ _content/tour/fre/context/answer1.go | 2 +- _content/tour/fre/context/example5.go | 2 +- _content/tour/ger/channels/example10.go | 2 +- _content/tour/ger/content_test.go | 89 +++++++++++++++++++++++++ _content/tour/ger/context/answer1.go | 2 +- _content/tour/ger/context/example5.go | 2 +- _content/tour/grc/channels/example10.go | 2 +- _content/tour/grc/content_test.go | 89 +++++++++++++++++++++++++ _content/tour/grc/context/answer1.go | 2 +- _content/tour/grc/context/example5.go | 2 +- _content/tour/ita/channels/example10.go | 2 +- _content/tour/ita/content_test.go | 89 +++++++++++++++++++++++++ _content/tour/ita/context/answer1.go | 2 +- _content/tour/ita/context/example5.go | 2 +- _content/tour/per/channels/example10.go | 2 +- _content/tour/per/content_test.go | 89 +++++++++++++++++++++++++ _content/tour/per/context/answer1.go | 2 +- _content/tour/per/context/example5.go | 2 +- _content/tour/pol/channels/example10.go | 2 +- _content/tour/pol/content_test.go | 89 +++++++++++++++++++++++++ _content/tour/pol/context/answer1.go | 2 +- _content/tour/pol/context/example5.go | 2 +- _content/tour/por/channels/example10.go | 2 +- _content/tour/por/content_test.go | 89 +++++++++++++++++++++++++ _content/tour/por/context/answer1.go | 2 +- _content/tour/por/context/example5.go | 2 +- _content/tour/rus/channels/example10.go | 2 +- _content/tour/rus/content_test.go | 89 +++++++++++++++++++++++++ _content/tour/rus/context/answer1.go | 2 +- _content/tour/rus/context/example5.go | 2 +- _content/tour/tur/channels/example10.go | 2 +- _content/tour/tur/content_test.go | 89 +++++++++++++++++++++++++ _content/tour/tur/context/answer1.go | 2 +- _content/tour/tur/context/example5.go | 2 +- 36 files changed, 828 insertions(+), 27 deletions(-) create mode 100644 _content/tour/fre/content_test.go create mode 100644 _content/tour/ger/content_test.go create mode 100644 _content/tour/grc/content_test.go create mode 100644 _content/tour/ita/content_test.go create mode 100644 _content/tour/per/content_test.go create mode 100644 _content/tour/pol/content_test.go create mode 100644 _content/tour/por/content_test.go create mode 100644 _content/tour/rus/content_test.go create mode 100644 _content/tour/tur/content_test.go diff --git a/_content/tour/fre/channels/example10.go b/_content/tour/fre/channels/example10.go index ed01faa0..e680c74a 100644 --- a/_content/tour/fre/channels/example10.go +++ b/_content/tour/fre/channels/example10.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/fre/content_test.go b/_content/tour/fre/content_test.go new file mode 100644 index 00000000..6c7b22d4 --- /dev/null +++ b/_content/tour/fre/content_test.go @@ -0,0 +1,89 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package content + +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) { + 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 + } + if filepath.Base(path) == "content_test.go" { + return nil + } + t.Run(path, func(t *testing.T) { + t.Parallel() + if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { + t.Errorf("%v: %v", path, err) + } + }) + return nil + }) + if err != nil { + t.Error(err) + } +} + +func testSnippet(t *testing.T, path, scratch string) error { + b, err := ioutil.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.Contains(build, "OMIT") { + return errors.New(`+build comment does not contain "OMIT"`) + } + + if strings.Contains(build, "no-build") { + return nil + } + bin := filepath.Join(scratch, filepath.Base(path)+".exe") + out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() + if err != nil { + return fmt.Errorf("build error: %v\noutput:\n%s", err, out) + } + defer os.Remove(bin) + + if strings.Contains(build, "no-run") { + return nil + } + out, err = exec.Command(bin).CombinedOutput() + if err != nil { + return fmt.Errorf("%v\nOutput:\n%s", err, out) + } + return nil +} diff --git a/_content/tour/fre/context/answer1.go b/_content/tour/fre/context/answer1.go index 49fca71b..8291bcf3 100644 --- a/_content/tour/fre/context/answer1.go +++ b/_content/tour/fre/context/answer1.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/fre/context/example5.go b/_content/tour/fre/context/example5.go index a04854fa..cf819766 100644 --- a/_content/tour/fre/context/example5.go +++ b/_content/tour/fre/context/example5.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/ger/channels/example10.go b/_content/tour/ger/channels/example10.go index ed01faa0..e680c74a 100644 --- a/_content/tour/ger/channels/example10.go +++ b/_content/tour/ger/channels/example10.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/ger/content_test.go b/_content/tour/ger/content_test.go new file mode 100644 index 00000000..6c7b22d4 --- /dev/null +++ b/_content/tour/ger/content_test.go @@ -0,0 +1,89 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package content + +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) { + 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 + } + if filepath.Base(path) == "content_test.go" { + return nil + } + t.Run(path, func(t *testing.T) { + t.Parallel() + if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { + t.Errorf("%v: %v", path, err) + } + }) + return nil + }) + if err != nil { + t.Error(err) + } +} + +func testSnippet(t *testing.T, path, scratch string) error { + b, err := ioutil.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.Contains(build, "OMIT") { + return errors.New(`+build comment does not contain "OMIT"`) + } + + if strings.Contains(build, "no-build") { + return nil + } + bin := filepath.Join(scratch, filepath.Base(path)+".exe") + out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() + if err != nil { + return fmt.Errorf("build error: %v\noutput:\n%s", err, out) + } + defer os.Remove(bin) + + if strings.Contains(build, "no-run") { + return nil + } + out, err = exec.Command(bin).CombinedOutput() + if err != nil { + return fmt.Errorf("%v\nOutput:\n%s", err, out) + } + return nil +} diff --git a/_content/tour/ger/context/answer1.go b/_content/tour/ger/context/answer1.go index b6febd74..8291bcf3 100644 --- a/_content/tour/ger/context/answer1.go +++ b/_content/tour/ger/context/answer1.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/ger/context/example5.go b/_content/tour/ger/context/example5.go index a04854fa..cf819766 100644 --- a/_content/tour/ger/context/example5.go +++ b/_content/tour/ger/context/example5.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/grc/channels/example10.go b/_content/tour/grc/channels/example10.go index e7c6e22d..9155ba82 100644 --- a/_content/tour/grc/channels/example10.go +++ b/_content/tour/grc/channels/example10.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // Όλα τα υλικά είναι αδειοδοτημένα υπό την Άδεια Apache Έκδοση 2.0, Ιανουάριος 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/grc/content_test.go b/_content/tour/grc/content_test.go new file mode 100644 index 00000000..6c7b22d4 --- /dev/null +++ b/_content/tour/grc/content_test.go @@ -0,0 +1,89 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package content + +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) { + 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 + } + if filepath.Base(path) == "content_test.go" { + return nil + } + t.Run(path, func(t *testing.T) { + t.Parallel() + if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { + t.Errorf("%v: %v", path, err) + } + }) + return nil + }) + if err != nil { + t.Error(err) + } +} + +func testSnippet(t *testing.T, path, scratch string) error { + b, err := ioutil.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.Contains(build, "OMIT") { + return errors.New(`+build comment does not contain "OMIT"`) + } + + if strings.Contains(build, "no-build") { + return nil + } + bin := filepath.Join(scratch, filepath.Base(path)+".exe") + out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() + if err != nil { + return fmt.Errorf("build error: %v\noutput:\n%s", err, out) + } + defer os.Remove(bin) + + if strings.Contains(build, "no-run") { + return nil + } + out, err = exec.Command(bin).CombinedOutput() + if err != nil { + return fmt.Errorf("%v\nOutput:\n%s", err, out) + } + return nil +} diff --git a/_content/tour/grc/context/answer1.go b/_content/tour/grc/context/answer1.go index a2dd4147..aa4f11c5 100644 --- a/_content/tour/grc/context/answer1.go +++ b/_content/tour/grc/context/answer1.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // Όλα τα υλικά είναι αδειοδοτημένα υπό την Άδεια Apache Έκδοση 2.0, Ιανουάριος 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/grc/context/example5.go b/_content/tour/grc/context/example5.go index 1cbcdb05..fce0c6ed 100644 --- a/_content/tour/grc/context/example5.go +++ b/_content/tour/grc/context/example5.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // Όλα τα υλικά είναι αδειοδοτημένα υπό την Άδεια Apache Έκδοση 2.0, Ιανουάριος 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/ita/channels/example10.go b/_content/tour/ita/channels/example10.go index ed01faa0..e680c74a 100644 --- a/_content/tour/ita/channels/example10.go +++ b/_content/tour/ita/channels/example10.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/ita/content_test.go b/_content/tour/ita/content_test.go new file mode 100644 index 00000000..6c7b22d4 --- /dev/null +++ b/_content/tour/ita/content_test.go @@ -0,0 +1,89 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package content + +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) { + 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 + } + if filepath.Base(path) == "content_test.go" { + return nil + } + t.Run(path, func(t *testing.T) { + t.Parallel() + if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { + t.Errorf("%v: %v", path, err) + } + }) + return nil + }) + if err != nil { + t.Error(err) + } +} + +func testSnippet(t *testing.T, path, scratch string) error { + b, err := ioutil.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.Contains(build, "OMIT") { + return errors.New(`+build comment does not contain "OMIT"`) + } + + if strings.Contains(build, "no-build") { + return nil + } + bin := filepath.Join(scratch, filepath.Base(path)+".exe") + out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() + if err != nil { + return fmt.Errorf("build error: %v\noutput:\n%s", err, out) + } + defer os.Remove(bin) + + if strings.Contains(build, "no-run") { + return nil + } + out, err = exec.Command(bin).CombinedOutput() + if err != nil { + return fmt.Errorf("%v\nOutput:\n%s", err, out) + } + return nil +} diff --git a/_content/tour/ita/context/answer1.go b/_content/tour/ita/context/answer1.go index 49fca71b..8291bcf3 100644 --- a/_content/tour/ita/context/answer1.go +++ b/_content/tour/ita/context/answer1.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/ita/context/example5.go b/_content/tour/ita/context/example5.go index a04854fa..cf819766 100644 --- a/_content/tour/ita/context/example5.go +++ b/_content/tour/ita/context/example5.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/per/channels/example10.go b/_content/tour/per/channels/example10.go index ed01faa0..e680c74a 100644 --- a/_content/tour/per/channels/example10.go +++ b/_content/tour/per/channels/example10.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/per/content_test.go b/_content/tour/per/content_test.go new file mode 100644 index 00000000..6c7b22d4 --- /dev/null +++ b/_content/tour/per/content_test.go @@ -0,0 +1,89 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package content + +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) { + 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 + } + if filepath.Base(path) == "content_test.go" { + return nil + } + t.Run(path, func(t *testing.T) { + t.Parallel() + if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { + t.Errorf("%v: %v", path, err) + } + }) + return nil + }) + if err != nil { + t.Error(err) + } +} + +func testSnippet(t *testing.T, path, scratch string) error { + b, err := ioutil.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.Contains(build, "OMIT") { + return errors.New(`+build comment does not contain "OMIT"`) + } + + if strings.Contains(build, "no-build") { + return nil + } + bin := filepath.Join(scratch, filepath.Base(path)+".exe") + out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() + if err != nil { + return fmt.Errorf("build error: %v\noutput:\n%s", err, out) + } + defer os.Remove(bin) + + if strings.Contains(build, "no-run") { + return nil + } + out, err = exec.Command(bin).CombinedOutput() + if err != nil { + return fmt.Errorf("%v\nOutput:\n%s", err, out) + } + return nil +} diff --git a/_content/tour/per/context/answer1.go b/_content/tour/per/context/answer1.go index 49fca71b..8291bcf3 100644 --- a/_content/tour/per/context/answer1.go +++ b/_content/tour/per/context/answer1.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/per/context/example5.go b/_content/tour/per/context/example5.go index a04854fa..cf819766 100644 --- a/_content/tour/per/context/example5.go +++ b/_content/tour/per/context/example5.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/pol/channels/example10.go b/_content/tour/pol/channels/example10.go index ed01faa0..e680c74a 100644 --- a/_content/tour/pol/channels/example10.go +++ b/_content/tour/pol/channels/example10.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/pol/content_test.go b/_content/tour/pol/content_test.go new file mode 100644 index 00000000..6c7b22d4 --- /dev/null +++ b/_content/tour/pol/content_test.go @@ -0,0 +1,89 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package content + +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) { + 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 + } + if filepath.Base(path) == "content_test.go" { + return nil + } + t.Run(path, func(t *testing.T) { + t.Parallel() + if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { + t.Errorf("%v: %v", path, err) + } + }) + return nil + }) + if err != nil { + t.Error(err) + } +} + +func testSnippet(t *testing.T, path, scratch string) error { + b, err := ioutil.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.Contains(build, "OMIT") { + return errors.New(`+build comment does not contain "OMIT"`) + } + + if strings.Contains(build, "no-build") { + return nil + } + bin := filepath.Join(scratch, filepath.Base(path)+".exe") + out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() + if err != nil { + return fmt.Errorf("build error: %v\noutput:\n%s", err, out) + } + defer os.Remove(bin) + + if strings.Contains(build, "no-run") { + return nil + } + out, err = exec.Command(bin).CombinedOutput() + if err != nil { + return fmt.Errorf("%v\nOutput:\n%s", err, out) + } + return nil +} diff --git a/_content/tour/pol/context/answer1.go b/_content/tour/pol/context/answer1.go index 49fca71b..8291bcf3 100644 --- a/_content/tour/pol/context/answer1.go +++ b/_content/tour/pol/context/answer1.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/pol/context/example5.go b/_content/tour/pol/context/example5.go index a04854fa..cf819766 100644 --- a/_content/tour/pol/context/example5.go +++ b/_content/tour/pol/context/example5.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/por/channels/example10.go b/_content/tour/por/channels/example10.go index ed01faa0..e680c74a 100644 --- a/_content/tour/por/channels/example10.go +++ b/_content/tour/por/channels/example10.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/por/content_test.go b/_content/tour/por/content_test.go new file mode 100644 index 00000000..6c7b22d4 --- /dev/null +++ b/_content/tour/por/content_test.go @@ -0,0 +1,89 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package content + +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) { + 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 + } + if filepath.Base(path) == "content_test.go" { + return nil + } + t.Run(path, func(t *testing.T) { + t.Parallel() + if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { + t.Errorf("%v: %v", path, err) + } + }) + return nil + }) + if err != nil { + t.Error(err) + } +} + +func testSnippet(t *testing.T, path, scratch string) error { + b, err := ioutil.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.Contains(build, "OMIT") { + return errors.New(`+build comment does not contain "OMIT"`) + } + + if strings.Contains(build, "no-build") { + return nil + } + bin := filepath.Join(scratch, filepath.Base(path)+".exe") + out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() + if err != nil { + return fmt.Errorf("build error: %v\noutput:\n%s", err, out) + } + defer os.Remove(bin) + + if strings.Contains(build, "no-run") { + return nil + } + out, err = exec.Command(bin).CombinedOutput() + if err != nil { + return fmt.Errorf("%v\nOutput:\n%s", err, out) + } + return nil +} diff --git a/_content/tour/por/context/answer1.go b/_content/tour/por/context/answer1.go index 49fca71b..8291bcf3 100644 --- a/_content/tour/por/context/answer1.go +++ b/_content/tour/por/context/answer1.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/por/context/example5.go b/_content/tour/por/context/example5.go index a04854fa..cf819766 100644 --- a/_content/tour/por/context/example5.go +++ b/_content/tour/por/context/example5.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/rus/channels/example10.go b/_content/tour/rus/channels/example10.go index ed01faa0..e680c74a 100644 --- a/_content/tour/rus/channels/example10.go +++ b/_content/tour/rus/channels/example10.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/rus/content_test.go b/_content/tour/rus/content_test.go new file mode 100644 index 00000000..6c7b22d4 --- /dev/null +++ b/_content/tour/rus/content_test.go @@ -0,0 +1,89 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package content + +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) { + 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 + } + if filepath.Base(path) == "content_test.go" { + return nil + } + t.Run(path, func(t *testing.T) { + t.Parallel() + if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { + t.Errorf("%v: %v", path, err) + } + }) + return nil + }) + if err != nil { + t.Error(err) + } +} + +func testSnippet(t *testing.T, path, scratch string) error { + b, err := ioutil.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.Contains(build, "OMIT") { + return errors.New(`+build comment does not contain "OMIT"`) + } + + if strings.Contains(build, "no-build") { + return nil + } + bin := filepath.Join(scratch, filepath.Base(path)+".exe") + out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() + if err != nil { + return fmt.Errorf("build error: %v\noutput:\n%s", err, out) + } + defer os.Remove(bin) + + if strings.Contains(build, "no-run") { + return nil + } + out, err = exec.Command(bin).CombinedOutput() + if err != nil { + return fmt.Errorf("%v\nOutput:\n%s", err, out) + } + return nil +} diff --git a/_content/tour/rus/context/answer1.go b/_content/tour/rus/context/answer1.go index 49fca71b..8291bcf3 100644 --- a/_content/tour/rus/context/answer1.go +++ b/_content/tour/rus/context/answer1.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/rus/context/example5.go b/_content/tour/rus/context/example5.go index a04854fa..cf819766 100644 --- a/_content/tour/rus/context/example5.go +++ b/_content/tour/rus/context/example5.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/tur/channels/example10.go b/_content/tour/tur/channels/example10.go index ed01faa0..e680c74a 100644 --- a/_content/tour/tur/channels/example10.go +++ b/_content/tour/tur/channels/example10.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/tur/content_test.go b/_content/tour/tur/content_test.go new file mode 100644 index 00000000..6c7b22d4 --- /dev/null +++ b/_content/tour/tur/content_test.go @@ -0,0 +1,89 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package content + +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) { + 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 + } + if filepath.Base(path) == "content_test.go" { + return nil + } + t.Run(path, func(t *testing.T) { + t.Parallel() + if err := testSnippet(t, filepath.ToSlash(path), scratch); err != nil { + t.Errorf("%v: %v", path, err) + } + }) + return nil + }) + if err != nil { + t.Error(err) + } +} + +func testSnippet(t *testing.T, path, scratch string) error { + b, err := ioutil.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.Contains(build, "OMIT") { + return errors.New(`+build comment does not contain "OMIT"`) + } + + if strings.Contains(build, "no-build") { + return nil + } + bin := filepath.Join(scratch, filepath.Base(path)+".exe") + out, err := exec.Command("go", "build", "-o", bin, path).CombinedOutput() + if err != nil { + return fmt.Errorf("build error: %v\noutput:\n%s", err, out) + } + defer os.Remove(bin) + + if strings.Contains(build, "no-run") { + return nil + } + out, err = exec.Command(bin).CombinedOutput() + if err != nil { + return fmt.Errorf("%v\nOutput:\n%s", err, out) + } + return nil +} diff --git a/_content/tour/tur/context/answer1.go b/_content/tour/tur/context/answer1.go index 49fca71b..8291bcf3 100644 --- a/_content/tour/tur/context/answer1.go +++ b/_content/tour/tur/context/answer1.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0 diff --git a/_content/tour/tur/context/example5.go b/_content/tour/tur/context/example5.go index a04854fa..cf819766 100644 --- a/_content/tour/tur/context/example5.go +++ b/_content/tour/tur/context/example5.go @@ -1,4 +1,4 @@ -//go:build OMIT || norun +//go:build OMIT // All material is licensed under the Apache License Version 2.0, January 2004 // http://www.apache.org/licenses/LICENSE-2.0