From 2a85f587d3109de5155f500c51042c6cd61dd18b Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sun, 27 Feb 2022 15:44:18 -0500 Subject: [PATCH 1/4] Fix a typo in blorg test --- blorg/config_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blorg/config_test.go b/blorg/config_test.go index 61e5749..979f875 100644 --- a/blorg/config_test.go +++ b/blorg/config_test.go @@ -14,7 +14,7 @@ func TestBlorg(t *testing.T) { t.Errorf("Could not read config: %s", err) return } - commitedHashBs, err := ioutil.ReadFile("testdata/public.md5") + committedHashBs, err := ioutil.ReadFile("testdata/public.md5") if err != nil { t.Errorf("Could not read hash bytes: %s", err) return @@ -28,7 +28,7 @@ func TestBlorg(t *testing.T) { t.Errorf("Could not hash PublicDir: %s", err) return } - rendered, committed := strings.TrimSpace(string(renderedHashBs)), strings.TrimSpace(string(commitedHashBs)) + rendered, committed := strings.TrimSpace(string(renderedHashBs)), strings.TrimSpace(string(committedHashBs)) if rendered != committed { t.Errorf("PublicDir hashes do not match: '%s' -> '%s'", committed, rendered) return From 1122a3dfbc7c1c3f95f5ca11c69d60234afbb5a1 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sun, 27 Feb 2022 15:44:52 -0500 Subject: [PATCH 2/4] Make blorg tests more verbose By not combining all files into one md5sum. --- blorg/config_test.go | 2 +- blorg/testdata/public.md5 | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/blorg/config_test.go b/blorg/config_test.go index 979f875..f75e10b 100644 --- a/blorg/config_test.go +++ b/blorg/config_test.go @@ -23,7 +23,7 @@ func TestBlorg(t *testing.T) { t.Errorf("Could not render: %s", err) return } - renderedHashBs, err := exec.Command("bash", "-c", fmt.Sprintf("find %s -type f | sort -u | xargs cat | md5sum", config.PublicDir)).Output() + renderedHashBs, err := exec.Command("bash", "-c", fmt.Sprintf("find %s -type f | sort -u | xargs md5sum", config.PublicDir)).Output() if err != nil { t.Errorf("Could not hash PublicDir: %s", err) return diff --git a/blorg/testdata/public.md5 b/blorg/testdata/public.md5 index 0cb9b58..d7506d5 100644 --- a/blorg/testdata/public.md5 +++ b/blorg/testdata/public.md5 @@ -1 +1,10 @@ -ab73939658896f4b21a1486bd7862751 - +a23f3b21b036af74ce0dc3b0f6a4d8d7 testdata/public/about.html +b93d8331258932e6bb18d866329b5e4e testdata/public/another-post.html +a4e5753838107f8cf44f8dfabc577c04 testdata/public/index.html +6e770ea67bb154191530585cc60c8c2f testdata/public/some-post.html +7a893b0b9b90974cd7d26cfcb0a22dd4 testdata/public/style.css +3ac91ccf813551d639daed7fae8689ae testdata/public/tags/another/index.html +f780413c40454793f50722300113f234 testdata/public/tags/some/index.html +967686d0550349659b60012f2449ef92 testdata/public/tags/static/index.html +2180a6f960a21f02c41028b2a2d418d6 testdata/public/tags/yet/index.html +be670bbbac7aec31b8f1563d944ea1ab testdata/public/yet-another-post/index.html From 65699c036b211130c1badbe1b61a3a9de177b810 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sun, 27 Feb 2022 19:15:48 -0500 Subject: [PATCH 3/4] Rewrite blorg tests without external md5sum It's a little bit longer, but avoids the need for external tools like find and md5sum. --- blorg/config_test.go | 75 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 12 deletions(-) diff --git a/blorg/config_test.go b/blorg/config_test.go index f75e10b..6c99d4a 100644 --- a/blorg/config_test.go +++ b/blorg/config_test.go @@ -1,36 +1,87 @@ package blorg import ( - "fmt" + "bufio" + "crypto/md5" + "encoding/hex" + "io/fs" "io/ioutil" - "os/exec" + "os" + "path/filepath" "strings" "testing" ) func TestBlorg(t *testing.T) { - config, err := ReadConfig("testdata/blorg.org") + // Re-generate this file with `find testdata/public -type f | sort -u | xargs md5sum > testdata/public.md5` + hashFile, err := os.Open("testdata/public.md5") if err != nil { - t.Errorf("Could not read config: %s", err) + t.Errorf("Could not open hash file: %s", err) + return + } + defer hashFile.Close() + scanner := bufio.NewScanner(hashFile) + committedHashes := make(map[string]string) + for scanner.Scan() { + parts := strings.Fields(scanner.Text()) + if len(parts) != 2 { + t.Errorf("Could not split hash entry line in 2: len(parts)=%d", len(parts)) + return + } + hash := parts[0] + fileName := parts[1] + committedHashes[fileName] = hash + } + if err := scanner.Err(); err != nil { + t.Errorf("Failed to read hash file: %s", err) return } - committedHashBs, err := ioutil.ReadFile("testdata/public.md5") + + config, err := ReadConfig("testdata/blorg.org") if err != nil { - t.Errorf("Could not read hash bytes: %s", err) + t.Errorf("Could not read config: %s", err) return } if err := config.Render(); err != nil { t.Errorf("Could not render: %s", err) return } - renderedHashBs, err := exec.Command("bash", "-c", fmt.Sprintf("find %s -type f | sort -u | xargs md5sum", config.PublicDir)).Output() + + renderedFileHashes := make(map[string]string) + err = filepath.WalkDir(config.PublicDir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() { + return nil + } + data, err := ioutil.ReadFile(path) + if err != nil { + return err + } + hash := md5.Sum(data) + renderedFileHashes[path] = hex.EncodeToString(hash[:]) + return nil + }) if err != nil { - t.Errorf("Could not hash PublicDir: %s", err) + t.Errorf("Could not determine hashes of rendered files: %s", err) return } - rendered, committed := strings.TrimSpace(string(renderedHashBs)), strings.TrimSpace(string(committedHashBs)) - if rendered != committed { - t.Errorf("PublicDir hashes do not match: '%s' -> '%s'", committed, rendered) - return + + for file, rendered := range renderedFileHashes { + if _, ok := committedHashes[file]; !ok { + t.Errorf("New file %s does not have a committed hash", file) + continue + } + committed := committedHashes[file] + committedHashes[file] = "" // To check if there are missing files later. + if rendered != committed { + t.Errorf("PublicDir hashes do not match for %s: '%s' -> '%s'", file, committed, rendered) + } + } + for file, committed := range committedHashes { + if committed != "" { + t.Errorf("Missing file %s has a committed hash, but was not rendered", file) + } } } From 2e821b8b7c41181d50feb5fafc0209741f6a9eda Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 28 Feb 2022 00:21:06 -0500 Subject: [PATCH 4/4] Update chroma to 0.10.0 - 0.9.2 added `tabindex="0"` to `pre`: https://github.com/alecthomas/chroma/pull/515 - 0.10.0 changed around divs and spans, pre and code https://github.com/alecthomas/chroma/pull/571 https://github.com/alecthomas/chroma/pull/572 --- blorg/testdata/public.md5 | 2 +- blorg/testdata/public/about.html | 208 +++++++++++++++---------------- go.mod | 7 +- go.sum | 36 ++---- 4 files changed, 117 insertions(+), 136 deletions(-) diff --git a/blorg/testdata/public.md5 b/blorg/testdata/public.md5 index d7506d5..3b653b8 100644 --- a/blorg/testdata/public.md5 +++ b/blorg/testdata/public.md5 @@ -1,4 +1,4 @@ -a23f3b21b036af74ce0dc3b0f6a4d8d7 testdata/public/about.html +2ca8531fdbcfcae02959ba8c810dda76 testdata/public/about.html b93d8331258932e6bb18d866329b5e4e testdata/public/another-post.html a4e5753838107f8cf44f8dfabc577c04 testdata/public/index.html 6e770ea67bb154191530585cc60c8c2f testdata/public/some-post.html diff --git a/blorg/testdata/public/about.html b/blorg/testdata/public/about.html index 5500a0e..d19d1e6 100644 --- a/blorg/testdata/public/about.html +++ b/blorg/testdata/public/about.html @@ -29,110 +29,110 @@

About This site is generated from go-org/blorg/testdata/content using the configuration in blorg.org

-
#+AUTHOR: testdata
-#+TITLE: blorg
-#+BASE_URL: /go-org/blorg
-#+OPTIONS: toc:nil title:nil
-#+CONTENT: ./content
-#+PUBLIC: ./public
-
-* templates
-** head
-#+name: head
-#+begin_src html
-<head>
-  <meta charset="utf-8">
-  <meta name="viewport" content="width=device-width, initial-scale=1" />
-  <link rel="stylesheet" href="/go-org/blorg/style.css" type="text/css" />
-  <title>{{ .Title }}</title>
-</head>
-#+end_src
-** header
-#+name: header
-#+begin_src html
-<header class='header'>
-  <a class="logo" href="/go-org/blorg">home</a>
-  <nav>
-    <a href="https://www.github.com/niklasfasching/go-org">github</a>
-  </nav>
-</header>
-#+end_src
-** item
-#+name: item
-#+begin_src html
-<!doctype html>
-<html>
-  {{ template "head" . }}
-  <body>
-    {{ template "header" . }}
-    <div class="container">
-      <h1 class="title">{{ .Title }}
-        <br>
-        <span class="subtitle">{{ .Subtitle }}</span>
-      </h1>
-      <ul class="tags">
-        {{ range .Tags }}
-        <li><a href="/go-org/blorg/tags/{{ . | Slugify }}">{{ . }}</a></li>
-        {{ end }}
-      </ul>
-      {{ .Content }}
-    </div>
-  </body>
-</html>
-#+end_src
-
-** list
-#+name: list
-#+begin_src html
-<!doctype html>
-<html>
-  {{ template "head" . }}
-  <body>
-    {{ template "header" . }}
-    <div class="container">
-      <h1 class="title">{{ .Title }}</h1>
-      <ul class="posts">
-        {{ range .Pages }}
-        <li class="post">
-          <a href="{{ .PermaLink }}">
-            <date>{{ .Date.Format "02.01.2006" }}</date>
-            <span>{{ .Title }}</span>
-          </a>
-        </li>
-        {{ end }}
-      </ul>
-      <ul>
-    </div>
-  </body>
-</html>
-#+end_src
-
-** index
-#+name: index
-#+begin_src html
-<!doctype html>
-<html>
-  {{ template "head" . }}
-  <body>
-    {{ template "header" . }}
-    <div class="container">
-      <h1 class="title">{{ .Title }}</h1>
-      <p>Only pages that have a date will be listed here - e.g. not <a href="about.html">about.html</a>
-      <ul class="posts">
-        {{ range .Pages }}
-        <li class="post">
-          <a href="{{ .PermaLink }}">
-            <date>{{ .Date.Format "02.01.2006" }}</date>
-            <span>{{ .Title }}</span>
-          </a>
-        </li>
-        {{ end }}
-      </ul>
-      <ul>
-    </div>
-  </body>
-</html>
-#+end_src
+
#+AUTHOR: testdata
+#+TITLE: blorg
+#+BASE_URL: /go-org/blorg
+#+OPTIONS: toc:nil title:nil
+#+CONTENT: ./content
+#+PUBLIC: ./public
+
+* templates
+** head
+#+name: head
+#+begin_src html
+<head>
+  <meta charset="utf-8">
+  <meta name="viewport" content="width=device-width, initial-scale=1" />
+  <link rel="stylesheet" href="/go-org/blorg/style.css" type="text/css" />
+  <title>{{ .Title }}</title>
+</head>
+#+end_src
+** header
+#+name: header
+#+begin_src html
+<header class='header'>
+  <a class="logo" href="/go-org/blorg">home</a>
+  <nav>
+    <a href="https://www.github.com/niklasfasching/go-org">github</a>
+  </nav>
+</header>
+#+end_src
+** item
+#+name: item
+#+begin_src html
+<!doctype html>
+<html>
+  {{ template "head" . }}
+  <body>
+    {{ template "header" . }}
+    <div class="container">
+      <h1 class="title">{{ .Title }}
+        <br>
+        <span class="subtitle">{{ .Subtitle }}</span>
+      </h1>
+      <ul class="tags">
+        {{ range .Tags }}
+        <li><a href="/go-org/blorg/tags/{{ . | Slugify }}">{{ . }}</a></li>
+        {{ end }}
+      </ul>
+      {{ .Content }}
+    </div>
+  </body>
+</html>
+#+end_src
+
+** list
+#+name: list
+#+begin_src html
+<!doctype html>
+<html>
+  {{ template "head" . }}
+  <body>
+    {{ template "header" . }}
+    <div class="container">
+      <h1 class="title">{{ .Title }}</h1>
+      <ul class="posts">
+        {{ range .Pages }}
+        <li class="post">
+          <a href="{{ .PermaLink }}">
+            <date>{{ .Date.Format "02.01.2006" }}</date>
+            <span>{{ .Title }}</span>
+          </a>
+        </li>
+        {{ end }}
+      </ul>
+      <ul>
+    </div>
+  </body>
+</html>
+#+end_src
+
+** index
+#+name: index
+#+begin_src html
+<!doctype html>
+<html>
+  {{ template "head" . }}
+  <body>
+    {{ template "header" . }}
+    <div class="container">
+      <h1 class="title">{{ .Title }}</h1>
+      <p>Only pages that have a date will be listed here - e.g. not <a href="about.html">about.html</a>
+      <ul class="posts">
+        {{ range .Pages }}
+        <li class="post">
+          <a href="{{ .PermaLink }}">
+            <date>{{ .Date.Format "02.01.2006" }}</date>
+            <span>{{ .Title }}</span>
+          </a>
+        </li>
+        {{ end }}
+      </ul>
+      <ul>
+    </div>
+  </body>
+</html>
+#+end_src
diff --git a/go.mod b/go.mod index bfff863..83b1fde 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,9 @@ module github.com/niklasfasching/go-org go 1.17 require ( - github.com/alecthomas/chroma v0.8.2 + github.com/alecthomas/chroma v0.10.0 github.com/pmezard/go-difflib v1.0.0 golang.org/x/net v0.0.0-20201224014010-6772e930b67b ) -require ( - github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect - github.com/dlclark/regexp2 v1.2.0 // indirect -) +require github.com/dlclark/regexp2 v1.4.0 // indirect diff --git a/go.sum b/go.sum index 827d544..ea8f04d 100644 --- a/go.sum +++ b/go.sum @@ -1,37 +1,21 @@ -github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U= -github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI= -github.com/alecthomas/chroma v0.8.2 h1:x3zkuE2lUk/RIekyAJ3XRqSCP4zwWDfcw/YJCuCAACg= -github.com/alecthomas/chroma v0.8.2/go.mod h1:sko8vR34/90zvl5QdcUdvzL3J8NKjAUx9va9jPuFNoM= -github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721 h1:JHZL0hZKJ1VENNfmXvHbgYlbUOvpzYzvy2aZU5gXVeo= -github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0= -github.com/alecthomas/kong v0.2.4/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE= -github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897 h1:p9Sln00KOTlrYkxI1zYWl1QLnEqAqEARBEYa8FQnQcY= -github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= -github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ= -github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk= +github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek= +github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk= -github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= -github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= +github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= -github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=