Skip to content

Commit e5dd77e

Browse files
committed
Embed testdata as physical Go code to try to get example_test working
Since using go:embed didn't work on pkg.go.dev, this is another attempt to get the examples to run on the docs website...
1 parent dc0d3c6 commit e5dd77e

File tree

10 files changed

+1322
-15
lines changed

10 files changed

+1322
-15
lines changed

flatgeobuf/example_test.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,37 @@ package flatgeobuf_test
66

77
import (
88
"bytes"
9-
"embed"
9+
"compress/bzip2"
10+
"encoding/base64"
1011
"encoding/hex"
1112
"fmt"
12-
"io/fs"
13+
"io"
1314
"sort"
15+
"strings"
1416

1517
"github.com/gogama/flatgeobuf/flatgeobuf"
1618
"github.com/gogama/flatgeobuf/flatgeobuf/flat"
19+
testdata "github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf"
1720
"github.com/gogama/flatgeobuf/packedrtree"
1821
flatbuffers "github.com/google/flatbuffers/go"
1922
)
2023

21-
//go:embed testdata/flatgeobuf/*.fgb
22-
var testFiles embed.FS
23-
24-
func openFile(name string) fs.File {
25-
f, err := testFiles.Open(name)
24+
func testdataReader(data string) io.Reader {
25+
var r io.Reader
26+
r = strings.NewReader(data)
27+
r = base64.NewDecoder(base64.StdEncoding, r)
28+
r = bzip2.NewReader(r)
29+
b, err := io.ReadAll(r)
2630
if err != nil {
2731
panic(err)
2832
}
29-
return f
33+
return bytes.NewReader(b)
3034
}
3135

3236
func ExampleMagic() {
33-
f := openFile("testdata/flatgeobuf/poly00.fgb")
34-
defer f.Close()
37+
var r = testdataReader(testdata.Poly00FGB)
3538

36-
version, err := flatgeobuf.Magic(f)
39+
version, err := flatgeobuf.Magic(r)
3740
fmt.Printf("%+v, %v\n", version, err)
3841
// Output: {Major:3 Patch:0}, <nil>
3942
}
@@ -44,7 +47,7 @@ func ExampleFileReader_emptyFile() {
4447
// index (but gets an error because the file has no index), and
4548
// reads the data section, which contains no features.
4649

47-
r := flatgeobuf.NewFileReader(openFile("testdata/flatgeobuf/empty.fgb"))
50+
r := flatgeobuf.NewFileReader(testdataReader(testdata.EmptyFGB))
4851
defer r.Close()
4952

5053
hdr, err := r.Header()
@@ -70,7 +73,7 @@ func ExampleFileReader_unknownFeatureCount() {
7073
// available features at once. It is equivalent to using Data() in
7174
// a loop until EOF is reached.
7275

73-
r := flatgeobuf.NewFileReader(openFile("testdata/flatgeobuf/unknown_feature_count.fgb"))
76+
r := flatgeobuf.NewFileReader(testdataReader(testdata.UnknownFeatureCountFGB))
7477
defer r.Close()
7578

7679
hdr, err := r.Header()
@@ -95,7 +98,7 @@ func ExampleFileReader_Index() {
9598
// reads the data section up to the first candidate feature and
9699
// prints a string summary of the candidate.
97100

98-
r := flatgeobuf.NewFileReader(openFile("testdata/flatgeobuf/countries.fgb"))
101+
r := flatgeobuf.NewFileReader(testdataReader(testdata.CountriesFGB))
99102
defer r.Close()
100103

101104
hdr, err := r.Header()
@@ -139,7 +142,7 @@ func ExampleFileReader_IndexSearch_streaming() {
139142
// all candidate features in a streaming manner, reading only the
140143
// minimum necessary data into memory.
141144

142-
r := flatgeobuf.NewFileReader(openFile("testdata/flatgeobuf/UScounties.fgb"))
145+
r := flatgeobuf.NewFileReader(testdataReader(testdata.UScountiesFGB))
143146
defer r.Close()
144147

145148
hdr, err := r.Header()

flatgeobuf/testdata/flatgeobuf/countries.go

Lines changed: 37 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package testdata
2+
3+
//go:generate ../../../script/testdata_embed.sh empty.fgb
4+
// DO NOT EDIT. Code generated by testdata_embed.sh version $Id$.
5+
//
6+
// This string contains the base64-encoded, bzip2-compressed, contents of the
7+
// file `empty.fgb`. For license information, please see
8+
// https://github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf/LICENSE.
9+
//
10+
// Last generated by schapper on Mon 18 Dec 2023 09:45:06 AM PST. The raw source
11+
// SHA-256 is bdc45e249e4418ef95a69b55a6db523f035231fef2c61dded2bb26fc9e64070d.
12+
//
13+
// This silly byte-embedding trick is needed to access the example test code
14+
// when running example code within the docs website,
15+
// https://pkg.go.dev/github.com/gogama/flatgeobuf.
16+
var EmptyFGB = `
17+
QlpoOTFBWSZTWa7whNgAAeD/+//9RFBUZ3/gPufe6r/v/7AQAEABBABAAUABAQAEAMACNwknYDjQ0DRpkaaNMgMTBAADQGgNMgMCZAyiTwKY0SeTSAAAAABkAA0AAAImiagBoBoAANAAGQAAAAAAJJCaGiNU2NUeQgPU0NDRoHqaaDTBHqZD1B6TaI9SftznzKfZSRJL/ERDO36FU3zSIixKrxKKBJSYMemE9Y047gE9ntIc4nZzJDM0GZoPRhYFWw5owcBC5B4Vtcda+DCX6YKmQbzYOZ3f0dOneoiZOnG9AYUY3JGN7sbC0sk/XFlGzAcm+uQDkAzRcc2L7yDaiO2zlEvSOScLqHmQfmr37u5VuzPPWDN7r7h+RmAc1DApsMiEQ3YXtTjHczI5hE4CkpRUlluJxRbZZAFcyxXErpfIZJQGsQtqLLygulUiNxgSGujKTaY4vLUqrtXWVCm7XEJqnFNbwGIzWVVKKahI38vZiJ7caDg4Jq5uGGAuyOM2nT1JSGg8lRr1pd0isrRpOEWWmzKuVNhAk7EQbPJshoFedkCldmAQQFE0JmKLoCDYqWHQSoNZWDaGrIEZXiwCzO8NoADYgIPN1ctigkIgJlVWnJoRIQy1IiACkfaKw6cCCiJiiIgIEEIsIDygLwT7lwT6hkDH/k2xtmsqDPt5zOC2J0H7fdLVQIlnsLqskY3BBeG8gv2ZKdkDAVblEhFrW6ma8c69paWQsVdaRPolxQsvu1LdUvYR3Vggp1SVkzCL09xmWKEme3WYRPZe1uevo8uRBiqvFt/F4sMF2L6Rdhfk9zVDYOVBC2aFy1z3ILuKcRhi6ach0XCKjS2yBRRJcTC0COnuaf9p/4u5IpwoSFd4QmwA
18+
`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package testdata
2+
3+
//go:generate ../../../script/testdata_embed.sh poly00.fgb
4+
// DO NOT EDIT. Code generated by testdata_embed.sh version $Id$.
5+
//
6+
// This string contains the base64-encoded, bzip2-compressed, contents of the
7+
// file `poly00.fgb`. For license information, please see
8+
// https://github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf/LICENSE.
9+
//
10+
// Last generated by schapper on Mon 18 Dec 2023 09:45:06 AM PST. The raw source
11+
// SHA-256 is 548a9ba3e51c089cb5c2501ccabd6de01884a57145f94a2b1be7f399fbf0c048.
12+
//
13+
// This silly byte-embedding trick is needed to access the example test code
14+
// when running example code within the docs website,
15+
// https://pkg.go.dev/github.com/gogama/flatgeobuf.
16+
var Poly00FGB = `
17+
QlpoOTFBWSZTWYyFnXoACyT////d9/P3///3///////v//Tvs7ffd3/f3t9z6ebu8/3c4Aj3z0KHcMYMwbgM0VhaqgADSQmhMaTJqbTTaSZqB5BGIPRGg0yMmIDQyDQ0NG1NNNoI0aMg0000NNBppkYjRieo00aDTTQBoaMmmmJkGopkEnmjU/QE9U0Y1Tw1E0ZPRMj1A0ANAAAAAAA08iaAADQNA0AAAAAAAAAAINGQaaA0aGgyNA0GgAAaaNGg0NNADQAGTTQDCNDQAADQGIADQAANABkA0ASapVTIxGgwJo0yaYATEaYCYQwBMjTIxDCAyaMAhgJk0wEyYTCaYRpphBhGmBMBMg0YTCDRkGmgNGhoMjQNBoAAGmjRoNDTQA0ABk00AwjQ0AAA0BiAA0AADQAZANAEkSaaEyTCZT1MEGQE1PFPFPSPFP0p6aYKepoyZ6mppp6jQ8TUHonqaGQ0PTU2ppoA0Bo9J6mhoAHkgaD1DQaHqPU9Q3qksAgJVFISGADvpw2kqZAlmtlOivYbyx3u+3/A4NlxmIIZEOlYhrHLfyNTmTV1gkiRvBDJS5M0EhgUGZtGoQ8ORM/Q+rEnbL2bpFYkBIQ7LivcZKsRMMIYiAA0JZ6UuNaoFs7khJSVFgpQCAxAkSNpQgDshAMoMJA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ft1v13Wgf7tTcPpkNouPRSu4Mq6yIAmi5J0aK7ztagNHkCKK54RhSzZ3JFgaYDUmBDMpnAIEg4nYS0R0Cj12vwm61hiUK1cWbXU0MN0WXmrgThbZRCZ7Wub6NAoMr2z2OnGY0yL4Y0plJXVCJDTmVH7E+G4atRP+1Lrokyvq8WF4trhV0IwLihJe2CNyBbuItmLOJlKRcbKuFG3L1FSnkoJpzMepEw6a6ZAytBVSxk2XMu5jUy0da5dEwAG3ZBYF85UXM5RZoGep3YYuKdiDyctqbuYmpYgVQKB0dItQBsgYVLJG4YBFo9XSSniUtBiawcchSoWgYZmrZphSidAJy0QHqj/GEYhzaCeVScYhPOQkTsH+WN2wmZmQrGBFxoMheYwFrf8PD7Ca/Zx/GLhbJyfQuvQMEwc2RA0cERrlRgaQOiU7E0BxudGREJ6nJ6ADHYHGQjAyFBk7AsM8WID0yaA4U/sg9UxIK5iJH7+jVdtKmXcwI1VM5H6BAHc9nm//F3JFOFCQjIWdegA==
18+
`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package testdata
2+
3+
//go:generate ../../../script/testdata_embed.sh unknown_feature_count.fgb
4+
// DO NOT EDIT. Code generated by testdata_embed.sh version $Id$.
5+
//
6+
// This string contains the base64-encoded, bzip2-compressed, contents of the
7+
// file `unknown_feature_count.fgb`. For license information, please see
8+
// https://github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf/LICENSE.
9+
//
10+
// Last generated by schapper on Mon 18 Dec 2023 09:45:06 AM PST. The raw source
11+
// SHA-256 is c104be29220ce555dc23339ab26ac1ca016172000aab1db225a5a23becb81fa0.
12+
//
13+
// This silly byte-embedding trick is needed to access the example test code
14+
// when running example code within the docs website,
15+
// https://pkg.go.dev/github.com/gogama/flatgeobuf.
16+
var UnknownFeatureCountFGB = `
17+
QlpoOTFBWSZTWT5whKcAAip////9RFBUb3/hfuf/6r/v/7AQEEABBABEAUyASIEBIgQEwAKWcYi26oNKaaQNT0anmiTamTQAYENDBGQNBkBhBkaAwjAaQT1TxNSeTUM0QAAAAAANAAAAGgAANNCVPU0G1GgD1AaABkAAAAAAAAAABJJGg0pmhAaaRoaA9TQBkekGRoBhH6kbU0NHqaGQ9Tyk7juhgIENrJMjmyqIhHgoUiTV/iBjC1JTo2NwtxCiZVHLgYYYmSAMyYlS8S1FY4E4i2KWNaqIiQLGlCqBKKkIhJckuwRJdmoRIyHVmWJXBRAwg7ASzGtgzmUxXZJTsihlwNostFldY0GLnC7TwceLzvJmDW4rsSSLSLrgCx+lHErLIZsLJRcsXtGvgVelVAVAX5HHDn78A1JB3fN4S8I5JwrCvIOCjZ0tejYfFyNrgzbOU4Kz0IACUNQCTVsUFDUjpStKbTvXzgpoEE8oNCea0tOigzSlSyRrNlADyiVTSxrWzZ6lxFFAA7wFQoVTGvQ+GlVEgjWaInlJIRB0MZJJcxpOJiYIWGuBbZzzAghSGvUAMSPtUUKRNMkYnI2t7SlFE26g29t9Z+5HllMDdO26WhKA0cRCbRyEumSKppJzaFVPTUr6a2BWf8HUzWa2sThNZlpTpMykZA9LKd3lZTt8GWTnSkLDnk+x+GYnkIZrmEBER3MgMqx4ZQADFASOLmOOt10CUiCIxOOur3JoRojRSI4MkOk0EJ821jLKSQQhFzNEL0L2T5y6+Hks7Q8n9TVLgTGDVwGAF0ZUH2irmWphEMFJXVMDnuCNdW0gGYHE9jABAVpvHwINJNZuz7s8JcgbC51yWWIg7+1ap10GYzLhk2x2bGgnuEKXsItS1mZZ8cGibOYRLTa0OLI1OzeQISrAaZ/g5JhKFooIhAOhVMyWYEmpxAHbUO/bMmYgH7jsItyOfKfke1Y/qlUsGxnWAhxqithGNxlyIxp15NiIhxIsI4P4Iw6ePFj/4u5IpwoSB84QlOA=
18+
`

flatgeobuf/testdata/flatgeobuf/uscounties.go

Lines changed: 1111 additions & 0 deletions
Large diffs are not rendered by default.

script/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testdata_embed.sh ident
File renamed without changes.

script/testdata_embed.sh

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2023 The flatgeobuf (Go) Authors. All rights reserved.
4+
# Use of this source code is governed by an MIT-style
5+
# license that can be found in the LICENSE file.
6+
7+
set -eo pipefail
8+
9+
10+
if ! command -v bzip2 >/dev/null; then
11+
>&2 << EOF cat
12+
error: bzip2 not found
13+
| This script uses bzip2 to compress test data files from
14+
| https://github.com/flatgeobuf/flatgeobuf prior to embedding
15+
| them in the Go code.
16+
|
17+
| Install bzip2 if you want to proceed.
18+
EOF
19+
exit 2
20+
fi
21+
22+
23+
function version() {
24+
local version='$Id$'
25+
version="${version##\$Id: }"
26+
version="${version%% \$}"
27+
echo $version
28+
}
29+
30+
31+
testdata_file="$1"
32+
if ! [ -f "$testdata_file" ]; then
33+
>&2 printf "unable to import %s: not found or not a file\n" "$testdata_file"
34+
fi
35+
36+
37+
# Derive the name of the string variable to contain the contents of the
38+
# testdata file.
39+
testdata_filename_root="$(basename "$testdata_file")"
40+
declare -a testdata_varname_parts
41+
testdata_filename_root="${testdata_filename_root%%.fgb}"
42+
readarray -td $'\n' testdata_varname_parts <<<"${testdata_filename_root//_/$'\n'}"
43+
testdata_varname=
44+
for (( i=0; i<"${#testdata_varname_parts}"; i++ )); do
45+
part="${testdata_varname_parts[$i]}"
46+
testdata_varname+="${part^}"
47+
done
48+
testdata_varname+=FGB
49+
50+
51+
# Create a temporary file to contain the input $GOFILE with generated
52+
# code inserted. Set a trap to make sure the file gets deleted if we
53+
# error out unexpectedly.
54+
tmp_file=""
55+
function cleanup() {
56+
rm -f "$tmp_file"
57+
}
58+
trap cleanup EXIT
59+
tmp_file=$(mktemp "$GOFILE.XXXXXXXX")
60+
61+
62+
# Print status.
63+
>&2 printf 'embedding %s into %s (%s) at line %d using %s\n' "$testdata_file" "$GOFILE" "var $testdata_varname" "$GOLINE" "$tmp_file"
64+
65+
# PREFIX: Echo every line of the Go file prior to the go:generate
66+
# directive to the temporary file.
67+
>"$tmp_file" head -n "$GOLINE" "$GOFILE"
68+
69+
70+
# INFIX: Insert the generated code.
71+
>>"$tmp_file" <<EOF cat
72+
// DO NOT EDIT. Code generated by testdata_embed.sh version $(version).
73+
//
74+
// This string contains the base64-encoded, bzip2-compressed, contents of the
75+
// file \`$testdata_file\`. For license information, please see
76+
// https://github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf/LICENSE.
77+
//
78+
// Last generated by $USER on $(date). The raw source
79+
// SHA-256 is $(sha256sum "$testdata_file" | cut -d' ' -f1).
80+
//
81+
// This silly byte-embedding trick is needed to access the example test code
82+
// when running example code within the docs website,
83+
// https://pkg.go.dev/github.com/gogama/flatgeobuf.
84+
var $testdata_varname = \`
85+
EOF
86+
<"$testdata_file" bzip2 | base64 -w 8192 >>"$tmp_file"
87+
>>"$tmp_file" echo '`'
88+
89+
90+
# Find the end of the string literal within the input file so we can
91+
# skip past it.
92+
offset="$(tail -n +"$GOLINE" "$GOFILE" | grep -n -m1 '\(^\|`\)`$' | cut -d: -f 1)"
93+
94+
95+
# SUFFIX: Echo every line of the Go file after the go:generate directive
96+
# to the temporary file.
97+
>>"$tmp_file" tail -n +$((GOLINE + offset)) "$GOFILE"
98+
99+
100+
# Swap the temporary file in for the original target Go file.
101+
mv "$tmp_file" "$GOFILE"
File renamed without changes.

0 commit comments

Comments
 (0)