From 77e73e382e1dfb20ece81bdf8014ad3c0794f344 Mon Sep 17 00:00:00 2001 From: jdkato Date: Wed, 13 Apr 2022 15:54:28 -0700 Subject: [PATCH] Add tests --- .circleci/config.yml | 51 +++++++++++++++++++++++++++++++++++ .vale.ini | 11 -------- Hugo/.vale.ini | 8 ++++++ go.mod | 10 +++++++ go.sum | 6 +++++ main.go | 8 ++++++ main_test.go | 46 +++++++++++++++++++++++++++++++ testdata/shortcodes/.vale.ini | 10 +++++++ testdata/shortcodes/test.md | 51 +++++++++++++++++++++++++++++++++++ testdata/test.ct | 13 +++++++++ 10 files changed, 203 insertions(+), 11 deletions(-) create mode 100644 .circleci/config.yml delete mode 100644 .vale.ini create mode 100644 Hugo/.vale.ini create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 main_test.go create mode 100644 testdata/shortcodes/.vale.ini create mode 100644 testdata/shortcodes/test.md create mode 100644 testdata/test.ct diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..93377f3 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,51 @@ +# Use the latest 2.1 version of CircleCI pipeline process engine. +# See: https://circleci.com/docs/2.0/configuration-reference +version: 2.1 + +# Define a job to be invoked later in a workflow. +# See: https://circleci.com/docs/2.0/configuration-reference/#jobs +jobs: + build: + working_directory: ~/repo + # Specify the execution environment. You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub. + # See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor + docker: + - image: circleci/golang:1.17 + # Add steps to the job + # See: https://circleci.com/docs/2.0/configuration-reference/#steps + steps: + - checkout + + - restore_cache: + keys: + - go-mod-v4-{{ checksum "go.sum" }} + + - run: + name: Install Dependencies + command: go mod download + + - run: + name: Fetch Vale + command: | + go install github.com/errata-ai/vale/v2/cmd/vale@cd147755b9fc6cf7d9ca1a70f4f75c633beeaf17 + echo 'export PATH=$PATH:$(go env GOPATH)/bin' >> $BASH_ENV + - save_cache: + key: go-mod-v4-{{ checksum "go.sum" }} + paths: + - "/go/pkg/mod" + + - run: + name: Run tests + command: | + mkdir -p /tmp/test-reports + gotestsum --junitfile /tmp/test-reports/unit-tests.xml + - store_test_results: + path: /tmp/test-reports + +# Invoke jobs via workflows +# See: https://circleci.com/docs/2.0/configuration-reference/#workflows +workflows: + sample: # This is the name of the workflow, feel free to change it to better match your workflow. + # Inside the workflow, you define the jobs you want to run. + jobs: + - build diff --git a/.vale.ini b/.vale.ini deleted file mode 100644 index 13f59e6..0000000 --- a/.vale.ini +++ /dev/null @@ -1,11 +0,0 @@ -[*.md] -# Exclude `{{< file >}}`, `{{< file-excerpt >}}`, `{{< output >}}`, -# and `{{< highlight ... >}}`. -# -# For a description (and unit tests) for these patterns see: -# https://regex101.com/r/m9klBv/3/tests -IgnorePatterns = (?s) *({{< output >}}.*?{{< ?/ ?output >}}), \ -(?s) *({{< ?file [^>]* ?>}}.*?{{< ?/ ?file ?>}}), \ -(?s) *({{< highlight [^>]* ?>}}.*?{{< ?/ ?highlight >}}), \ -(?s) *({{< image .*? >}}), \ -(?s) *({{< content .*? >}}) diff --git a/Hugo/.vale.ini b/Hugo/.vale.ini new file mode 100644 index 0000000..f145fe1 --- /dev/null +++ b/Hugo/.vale.ini @@ -0,0 +1,8 @@ +[*.md] +# Exclude `{{< ... >}}`, `{{% ... %}}`, [Who]({{< ... >}}) +TokenIgnores = ({{[%<] .* [%>]}}.*?{{[%<] ?/.* [%>]}}), \ +(\[\w+\]\({{< .+ >}}\)) + +# Exclude `{{< myshortcode `This is some HTML, ... >}}` +BlockIgnores = (?sm)^({{[%<] [^{]*? [%>]}})\n$, \ +(?s) *({{< highlight [^>]* ?>}}.*?{{< ?/ ?highlight >}}) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ade82c3 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/errata-ai/Hugo + +go 1.17 + +require github.com/google/go-cmdtest v0.4.0 + +require ( + github.com/google/go-cmp v0.3.1 // indirect + github.com/google/renameio v0.1.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..57cabf1 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/google/go-cmdtest v0.4.0 h1:ToXh6W5spLp3npJV92tk6d5hIpUPYEzHLkD+rncbyhI= +github.com/google/go-cmdtest v0.4.0/go.mod h1:apVn/GCasLZUVpAJ6oWAuyP7Ne7CEsQbTnc0plM3m+o= +github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/renameio v0.1.0 h1:GOZbcHa3HfsPKPlmyPyN2KEohoMXOhdMbHrvbpl2QaA= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..2ce8d07 --- /dev/null +++ b/main.go @@ -0,0 +1,8 @@ +package main + +import "os" + +func cdf() int { + os.Chdir(os.Args[1]) + return 0 +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..b894d0e --- /dev/null +++ b/main_test.go @@ -0,0 +1,46 @@ +package main + +import ( + "flag" + "fmt" + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" + + "github.com/google/go-cmdtest" +) + +var update = flag.Bool("update", false, "replace test file contents with output") + +func Test(t *testing.T) { + ts, err := cmdtest.Read("testdata") + if err != nil { + t.Fatal(err) + } + + ts.Setup = func(_ string) error { + _, testFileName, _, ok := runtime.Caller(0) + if !ok { + return fmt.Errorf("failed get real working directory from caller") + } + + projectRootDir := filepath.Join(filepath.Dir(testFileName), "testdata") + if err := os.Setenv("ROOTDIR", projectRootDir); err != nil { + return fmt.Errorf("failed change 'ROOTDIR' to caller working directory: %v", err) + } + + return nil + } + + path, err := exec.LookPath("vale") + if err != nil { + t.Fatal(err) + } + + ts.Commands["vale"] = cmdtest.Program(path) + ts.Commands["cdf"] = cmdtest.InProcessProgram("cdf", cdf) + + ts.Run(t, *update) +} diff --git a/testdata/shortcodes/.vale.ini b/testdata/shortcodes/.vale.ini new file mode 100644 index 0000000..118a18d --- /dev/null +++ b/testdata/shortcodes/.vale.ini @@ -0,0 +1,10 @@ +[*.md] +BasedOnStyles = Vale + +# Exclude `{{< ... >}}`, `{{% ... %}}`, [Who]({{< ... >}}) +TokenIgnores = ({{[%<] .* [%>]}}.*?{{[%<] ?/.* [%>]}}), \ +(\[\w+\]\({{< .+ >}}\)) + +# Exclude `{{< myshortcode `This is some HTML, ... >}}` +BlockIgnores = (?sm)^({{[%<] [^{]*? [%>]}})\n$, \ +(?s) *({{< highlight [^>]* ?>}}.*?{{< ?/ ?highlight >}}) diff --git a/testdata/shortcodes/test.md b/testdata/shortcodes/test.md new file mode 100644 index 0000000..5e586b3 --- /dev/null +++ b/testdata/shortcodes/test.md @@ -0,0 +1,51 @@ +# Shortcodes + +> Shortcodes are simple snippets inside your content files calling built-in or +> custom templates. + +{{% mdshortcode %}}Stuff to `process` in the *center*.{{% /mdshortcode %}} + +Shortcodes are non-standard markup that appears within normal Markdown. + +{{< highlt go >}} A bunch of code here {{< /highlt >}} + +Shortcodes are non-standard markup that appears within normal Markdown. + +{{< myshortcode This is some HTML, +and a new line with a "quoted string". >}} + +Shortcodes are non-standard markup that appears within normal Markdown. + +{{< myshortcode This is some HTML,. >}} + +Shortcodes are non-standard markup that appears within normal Markdown. + +{{< myshortcode src="/media/spf13.jpg" title="Steve Francia" >}} + +Shortcodes are non-standard markup that appears within normal Markdown. + +{{< highlight html >}} +
+
+

{{ .Title }}

+ {{ range .Pages }} + {{ .Render "summary"}} + {{ end }} +
+
+{{< /highlight >}} + +Shortcodes are non-standard markup that appears within normal Markdown. + +{{< instagram BWNjjyYFxVx hidecaption >}} + +Shortcodes are non-standard markup that appears within normal Markdown. + +[Who]({{< relref "about.md#who" >}}) + +hidecaption + +{{< youtube id="w7Ft2ymGmfc" autoplay="true" >}} + +Shortcodes are non-standard markup that appears within normal Markdown. + diff --git a/testdata/test.ct b/testdata/test.ct new file mode 100644 index 0000000..fe3e80c --- /dev/null +++ b/testdata/test.ct @@ -0,0 +1,13 @@ +$ cdf ${ROOTDIR}/shortcodes +$ vale --output=line --sort --normalize --relative --no-exit test.md +test.md:1:3:Vale.Spelling:Did you really mean 'Shortcodes'? +test.md:3:3:Vale.Spelling:Did you really mean 'Shortcodes'? +test.md:8:1:Vale.Spelling:Did you really mean 'Shortcodes'? +test.md:12:1:Vale.Spelling:Did you really mean 'Shortcodes'? +test.md:17:1:Vale.Spelling:Did you really mean 'Shortcodes'? +test.md:21:1:Vale.Spelling:Did you really mean 'Shortcodes'? +test.md:25:1:Vale.Spelling:Did you really mean 'Shortcodes'? +test.md:38:1:Vale.Spelling:Did you really mean 'Shortcodes'? +test.md:42:1:Vale.Spelling:Did you really mean 'Shortcodes'? +test.md:46:1:Vale.Spelling:Did you really mean 'hidecaption'? +test.md:50:1:Vale.Spelling:Did you really mean 'Shortcodes'?