Skip to content

Commit

Permalink
Embed testdata as physical Go code to try to get example_test working
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
vcschapp committed Dec 18, 2023
1 parent dc0d3c6 commit e5dd77e
Show file tree
Hide file tree
Showing 10 changed files with 1,322 additions and 15 deletions.
33 changes: 18 additions & 15 deletions flatgeobuf/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,37 @@ package flatgeobuf_test

import (
"bytes"
"embed"
"compress/bzip2"
"encoding/base64"
"encoding/hex"
"fmt"
"io/fs"
"io"
"sort"
"strings"

"github.com/gogama/flatgeobuf/flatgeobuf"
"github.com/gogama/flatgeobuf/flatgeobuf/flat"
testdata "github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf"
"github.com/gogama/flatgeobuf/packedrtree"
flatbuffers "github.com/google/flatbuffers/go"
)

//go:embed testdata/flatgeobuf/*.fgb
var testFiles embed.FS

func openFile(name string) fs.File {
f, err := testFiles.Open(name)
func testdataReader(data string) io.Reader {
var r io.Reader
r = strings.NewReader(data)
r = base64.NewDecoder(base64.StdEncoding, r)
r = bzip2.NewReader(r)
b, err := io.ReadAll(r)
if err != nil {
panic(err)
}
return f
return bytes.NewReader(b)
}

func ExampleMagic() {
f := openFile("testdata/flatgeobuf/poly00.fgb")
defer f.Close()
var r = testdataReader(testdata.Poly00FGB)

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

r := flatgeobuf.NewFileReader(openFile("testdata/flatgeobuf/empty.fgb"))
r := flatgeobuf.NewFileReader(testdataReader(testdata.EmptyFGB))
defer r.Close()

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

r := flatgeobuf.NewFileReader(openFile("testdata/flatgeobuf/unknown_feature_count.fgb"))
r := flatgeobuf.NewFileReader(testdataReader(testdata.UnknownFeatureCountFGB))
defer r.Close()

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

r := flatgeobuf.NewFileReader(openFile("testdata/flatgeobuf/countries.fgb"))
r := flatgeobuf.NewFileReader(testdataReader(testdata.CountriesFGB))
defer r.Close()

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

r := flatgeobuf.NewFileReader(openFile("testdata/flatgeobuf/UScounties.fgb"))
r := flatgeobuf.NewFileReader(testdataReader(testdata.UScountiesFGB))
defer r.Close()

hdr, err := r.Header()
Expand Down
37 changes: 37 additions & 0 deletions flatgeobuf/testdata/flatgeobuf/countries.go

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions flatgeobuf/testdata/flatgeobuf/empty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package testdata

//go:generate ../../../script/testdata_embed.sh empty.fgb
// DO NOT EDIT. Code generated by testdata_embed.sh version $Id$.
//
// This string contains the base64-encoded, bzip2-compressed, contents of the
// file `empty.fgb`. For license information, please see
// https://github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf/LICENSE.
//
// Last generated by schapper on Mon 18 Dec 2023 09:45:06 AM PST. The raw source
// SHA-256 is bdc45e249e4418ef95a69b55a6db523f035231fef2c61dded2bb26fc9e64070d.
//
// This silly byte-embedding trick is needed to access the example test code
// when running example code within the docs website,
// https://pkg.go.dev/github.com/gogama/flatgeobuf.
var EmptyFGB = `
QlpoOTFBWSZTWa7whNgAAeD/+//9RFBUZ3/gPufe6r/v/7AQAEABBABAAUABAQAEAMACNwknYDjQ0DRpkaaNMgMTBAADQGgNMgMCZAyiTwKY0SeTSAAAAABkAA0AAAImiagBoBoAANAAGQAAAAAAJJCaGiNU2NUeQgPU0NDRoHqaaDTBHqZD1B6TaI9SftznzKfZSRJL/ERDO36FU3zSIixKrxKKBJSYMemE9Y047gE9ntIc4nZzJDM0GZoPRhYFWw5owcBC5B4Vtcda+DCX6YKmQbzYOZ3f0dOneoiZOnG9AYUY3JGN7sbC0sk/XFlGzAcm+uQDkAzRcc2L7yDaiO2zlEvSOScLqHmQfmr37u5VuzPPWDN7r7h+RmAc1DApsMiEQ3YXtTjHczI5hE4CkpRUlluJxRbZZAFcyxXErpfIZJQGsQtqLLygulUiNxgSGujKTaY4vLUqrtXWVCm7XEJqnFNbwGIzWVVKKahI38vZiJ7caDg4Jq5uGGAuyOM2nT1JSGg8lRr1pd0isrRpOEWWmzKuVNhAk7EQbPJshoFedkCldmAQQFE0JmKLoCDYqWHQSoNZWDaGrIEZXiwCzO8NoADYgIPN1ctigkIgJlVWnJoRIQy1IiACkfaKw6cCCiJiiIgIEEIsIDygLwT7lwT6hkDH/k2xtmsqDPt5zOC2J0H7fdLVQIlnsLqskY3BBeG8gv2ZKdkDAVblEhFrW6ma8c69paWQsVdaRPolxQsvu1LdUvYR3Vggp1SVkzCL09xmWKEme3WYRPZe1uevo8uRBiqvFt/F4sMF2L6Rdhfk9zVDYOVBC2aFy1z3ILuKcRhi6ach0XCKjS2yBRRJcTC0COnuaf9p/4u5IpwoSFd4QmwA
`
18 changes: 18 additions & 0 deletions flatgeobuf/testdata/flatgeobuf/poly00.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package testdata

//go:generate ../../../script/testdata_embed.sh poly00.fgb
// DO NOT EDIT. Code generated by testdata_embed.sh version $Id$.
//
// This string contains the base64-encoded, bzip2-compressed, contents of the
// file `poly00.fgb`. For license information, please see
// https://github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf/LICENSE.
//
// Last generated by schapper on Mon 18 Dec 2023 09:45:06 AM PST. The raw source
// SHA-256 is 548a9ba3e51c089cb5c2501ccabd6de01884a57145f94a2b1be7f399fbf0c048.
//
// This silly byte-embedding trick is needed to access the example test code
// when running example code within the docs website,
// https://pkg.go.dev/github.com/gogama/flatgeobuf.
var Poly00FGB = `
QlpoOTFBWSZTWYyFnXoACyT////d9/P3///3///////v//Tvs7ffd3/f3t9z6ebu8/3c4Aj3z0KHcMYMwbgM0VhaqgADSQmhMaTJqbTTaSZqB5BGIPRGg0yMmIDQyDQ0NG1NNNoI0aMg0000NNBppkYjRieo00aDTTQBoaMmmmJkGopkEnmjU/QE9U0Y1Tw1E0ZPRMj1A0ANAAAAAAA08iaAADQNA0AAAAAAAAAAINGQaaA0aGgyNA0GgAAaaNGg0NNADQAGTTQDCNDQAADQGIADQAANABkA0ASapVTIxGgwJo0yaYATEaYCYQwBMjTIxDCAyaMAhgJk0wEyYTCaYRpphBhGmBMBMg0YTCDRkGmgNGhoMjQNBoAAGmjRoNDTQA0ABk00AwjQ0AAA0BiAA0AADQAZANAEkSaaEyTCZT1MEGQE1PFPFPSPFP0p6aYKepoyZ6mppp6jQ8TUHonqaGQ0PTU2ppoA0Bo9J6mhoAHkgaD1DQaHqPU9Q3qksAgJVFISGADvpw2kqZAlmtlOivYbyx3u+3/A4NlxmIIZEOlYhrHLfyNTmTV1gkiRvBDJS5M0EhgUGZtGoQ8ORM/Q+rEnbL2bpFYkBIQ7LivcZKsRMMIYiAA0JZ6UuNaoFs7khJSVFgpQCAxAkSNpQgDshAMoMJAzJIgyQt9v98+/12LXcEoEeqYWWehZneyHwpkkI7thGoZAosJCLjJCB2EIGYQIXfMJAZQjHcSdsTGGcRARaEMIjERMkMIzmAJkLOcWSwnYpYSEbX27oPHZIwITfIQgVdISKyIskgHiMjruqjhoDMx2hHXsuAEU4Xr2TiETCWk0epAFpb76/ZS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F4trhV0IwLihJe2CNyBbuItmLOJlKRcbKuFG3L1FSnkoJpzMepEw6a6ZAytBVSxk2XMu5jUy0da5dEwAG3ZBYF85UXM5RZoGep3YYuKdiDyctqbuYmpYgVQKB0dItQBsgYVLJG4YBFo9XSSniUtBiawcchSoWgYZmrZphSidAJy0QHqj/GEYhzaCeVScYhPOQkTsH+WN2wmZmQrGBFxoMheYwFrf8PD7Ca/Zx/GLhbJyfQuvQMEwc2RA0cERrlRgaQOiU7E0BxudGREJ6nJ6ADHYHGQjAyFBk7AsM8WID0yaA4U/sg9UxIK5iJH7+jVdtKmXcwI1VM5H6BAHc9nm//F3JFOFCQjIWdegA==
`
18 changes: 18 additions & 0 deletions flatgeobuf/testdata/flatgeobuf/unknown_feature_count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package testdata

//go:generate ../../../script/testdata_embed.sh unknown_feature_count.fgb
// DO NOT EDIT. Code generated by testdata_embed.sh version $Id$.
//
// This string contains the base64-encoded, bzip2-compressed, contents of the
// file `unknown_feature_count.fgb`. For license information, please see
// https://github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf/LICENSE.
//
// Last generated by schapper on Mon 18 Dec 2023 09:45:06 AM PST. The raw source
// SHA-256 is c104be29220ce555dc23339ab26ac1ca016172000aab1db225a5a23becb81fa0.
//
// This silly byte-embedding trick is needed to access the example test code
// when running example code within the docs website,
// https://pkg.go.dev/github.com/gogama/flatgeobuf.
var UnknownFeatureCountFGB = `
QlpoOTFBWSZTWT5whKcAAip////9RFBUb3/hfuf/6r/v/7AQEEABBABEAUyASIEBIgQEwAKWcYi26oNKaaQNT0anmiTamTQAYENDBGQNBkBhBkaAwjAaQT1TxNSeTUM0QAAAAAANAAAAGgAANNCVPU0G1GgD1AaABkAAAAAAAAAABJJGg0pmhAaaRoaA9TQBkekGRoBhH6kbU0NHqaGQ9Tyk7juhgIENrJMjmyqIhHgoUiTV/iBjC1JTo2NwtxCiZVHLgYYYmSAMyYlS8S1FY4E4i2KWNaqIiQLGlCqBKKkIhJckuwRJdmoRIyHVmWJXBRAwg7ASzGtgzmUxXZJTsihlwNostFldY0GLnC7TwceLzvJmDW4rsSSLSLrgCx+lHErLIZsLJRcsXtGvgVelVAVAX5HHDn78A1JB3fN4S8I5JwrCvIOCjZ0tejYfFyNrgzbOU4Kz0IACUNQCTVsUFDUjpStKbTvXzgpoEE8oNCea0tOigzSlSyRrNlADyiVTSxrWzZ6lxFFAA7wFQoVTGvQ+GlVEgjWaInlJIRB0MZJJcxpOJiYIWGuBbZzzAghSGvUAMSPtUUKRNMkYnI2t7SlFE26g29t9Z+5HllMDdO26WhKA0cRCbRyEumSKppJzaFVPTUr6a2BWf8HUzWa2sThNZlpTpMykZA9LKd3lZTt8GWTnSkLDnk+x+GYnkIZrmEBER3MgMqx4ZQADFASOLmOOt10CUiCIxOOur3JoRojRSI4MkOk0EJ821jLKSQQhFzNEL0L2T5y6+Hks7Q8n9TVLgTGDVwGAF0ZUH2irmWphEMFJXVMDnuCNdW0gGYHE9jABAVpvHwINJNZuz7s8JcgbC51yWWIg7+1ap10GYzLhk2x2bGgnuEKXsItS1mZZ8cGibOYRLTa0OLI1OzeQISrAaZ/g5JhKFooIhAOhVMyWYEmpxAHbUO/bMmYgH7jsItyOfKfke1Y/qlUsGxnWAhxqithGNxlyIxp15NiIhxIsI4P4Iw6ePFj/4u5IpwoSB84QlOA=
`
1,111 changes: 1,111 additions & 0 deletions flatgeobuf/testdata/flatgeobuf/uscounties.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions script/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata_embed.sh ident
File renamed without changes.
101 changes: 101 additions & 0 deletions script/testdata_embed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env bash

# Copyright 2023 The flatgeobuf (Go) Authors. All rights reserved.
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file.

set -eo pipefail


if ! command -v bzip2 >/dev/null; then
>&2 << EOF cat
error: bzip2 not found
| This script uses bzip2 to compress test data files from
| https://github.com/flatgeobuf/flatgeobuf prior to embedding
| them in the Go code.
|
| Install bzip2 if you want to proceed.
EOF
exit 2
fi


function version() {
local version='$Id$'
version="${version##\$Id: }"
version="${version%% \$}"
echo $version
}


testdata_file="$1"
if ! [ -f "$testdata_file" ]; then
>&2 printf "unable to import %s: not found or not a file\n" "$testdata_file"
fi


# Derive the name of the string variable to contain the contents of the
# testdata file.
testdata_filename_root="$(basename "$testdata_file")"
declare -a testdata_varname_parts
testdata_filename_root="${testdata_filename_root%%.fgb}"
readarray -td $'\n' testdata_varname_parts <<<"${testdata_filename_root//_/$'\n'}"
testdata_varname=
for (( i=0; i<"${#testdata_varname_parts}"; i++ )); do
part="${testdata_varname_parts[$i]}"
testdata_varname+="${part^}"
done
testdata_varname+=FGB


# Create a temporary file to contain the input $GOFILE with generated
# code inserted. Set a trap to make sure the file gets deleted if we
# error out unexpectedly.
tmp_file=""
function cleanup() {
rm -f "$tmp_file"
}
trap cleanup EXIT
tmp_file=$(mktemp "$GOFILE.XXXXXXXX")


# Print status.
>&2 printf 'embedding %s into %s (%s) at line %d using %s\n' "$testdata_file" "$GOFILE" "var $testdata_varname" "$GOLINE" "$tmp_file"

# PREFIX: Echo every line of the Go file prior to the go:generate
# directive to the temporary file.
>"$tmp_file" head -n "$GOLINE" "$GOFILE"


# INFIX: Insert the generated code.
>>"$tmp_file" <<EOF cat
// DO NOT EDIT. Code generated by testdata_embed.sh version $(version).
//
// This string contains the base64-encoded, bzip2-compressed, contents of the
// file \`$testdata_file\`. For license information, please see
// https://github.com/gogama/flatgeobuf/flatgeobuf/testdata/flatgeobuf/LICENSE.
//
// Last generated by $USER on $(date). The raw source
// SHA-256 is $(sha256sum "$testdata_file" | cut -d' ' -f1).
//
// This silly byte-embedding trick is needed to access the example test code
// when running example code within the docs website,
// https://pkg.go.dev/github.com/gogama/flatgeobuf.
var $testdata_varname = \`
EOF
<"$testdata_file" bzip2 | base64 -w 8192 >>"$tmp_file"
>>"$tmp_file" echo '`'


# Find the end of the string literal within the input file so we can
# skip past it.
offset="$(tail -n +"$GOLINE" "$GOFILE" | grep -n -m1 '\(^\|`\)`$' | cut -d: -f 1)"


# SUFFIX: Echo every line of the Go file after the go:generate directive
# to the temporary file.
>>"$tmp_file" tail -n +$((GOLINE + offset)) "$GOFILE"


# Swap the temporary file in for the original target Go file.
mv "$tmp_file" "$GOFILE"
File renamed without changes.

0 comments on commit e5dd77e

Please sign in to comment.