check / check (push) Waiting to run
Package mfer handed out a loaded manifest as the unexported type manifest. Without a name for that type, a program using the library could not keep a loaded manifest in a struct field or pass one to its own functions. As sneak approved, the type is now exported as Manifest with its fields kept private. Its existing methods (Files, Signer, CreatedAt, String) are its whole API. NewManifestFromReader and NewManifestFromFile return *Manifest. The two lint suppressions that excused returning an unexported type from them are removed. The type's doc comment now names the two loaders and drops the pointer to the settled design question. String gets a doc comment. A test in package mfer_test uses *mfer.Manifest from outside the package. Model: opus-5-5
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package mfer_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"sneak.berlin/go/mfer/mfer"
|
|
)
|
|
|
|
// archive keeps a loaded manifest in a field, as a type in a program that
|
|
// imports mfer would.
|
|
type archive struct {
|
|
manifest *mfer.Manifest
|
|
}
|
|
|
|
// fileCount takes a loaded manifest, as a function in such a program would.
|
|
func fileCount(m *mfer.Manifest) int {
|
|
return len(m.Files())
|
|
}
|
|
|
|
// A program that imports mfer can name the type of a loaded manifest, keep
|
|
// one in a struct field, pass it to its own functions, and read it through
|
|
// its methods.
|
|
func TestManifestIsUsableOutsideThePackage(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
content := []byte("hello\n")
|
|
builder := mfer.NewBuilder()
|
|
|
|
_, err := builder.AddFile("hello.txt", mfer.FileSize(len(content)),
|
|
mfer.ModTime{}, 0, bytes.NewReader(content), nil)
|
|
require.NoError(t, err)
|
|
|
|
var written bytes.Buffer
|
|
require.NoError(t, builder.Build(context.Background(), &written))
|
|
|
|
loaded, err := mfer.NewManifestFromReader(&written)
|
|
require.NoError(t, err)
|
|
|
|
a := archive{manifest: loaded}
|
|
|
|
require.Equal(t, 1, fileCount(a.manifest))
|
|
require.Equal(t, "hello.txt", a.manifest.Files()[0].GetPath())
|
|
require.Nil(t, a.manifest.Signer())
|
|
require.Nil(t, a.manifest.CreatedAt())
|
|
require.Equal(t, "<Manifest count=1>", a.manifest.String())
|
|
}
|