delegationtest: make the generator a main()

This commit is contained in:
Michael Muré
2024-11-20 15:35:33 +01:00
parent aea1880386
commit c19e38356d
5 changed files with 30 additions and 40 deletions

View File

@@ -22,7 +22,7 @@
// tokens stored in the data/ directory should be regenerated by running
// the following command in this directory:
//
// go test . -update
// cd generator && go run .
//
// Generated delegation Tokens are stored in the data/ directory and loaded
// into the delegation.Loader.

View File

@@ -1,4 +1,4 @@
package delegationtest
package main
import (
"os"
@@ -15,11 +15,13 @@ import (
"github.com/ucan-wg/go-ucan/pkg/command"
"github.com/ucan-wg/go-ucan/pkg/policy"
"github.com/ucan-wg/go-ucan/token/delegation"
"github.com/ucan-wg/go-ucan/token/delegation/delegationtest"
)
const (
tokenNamePrefix = "Token"
proorChainNamePrefix = "Proof"
tokenExt = ".dagcbor"
)
var constantNonce = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b}
@@ -83,7 +85,7 @@ func (g *generator) chainPersonas(personas []didtest.Persona, acc acc, vari vari
params := newDelegationParams{
privKey: personas[0].PrivKey(),
aud: personas[1].DID(),
cmd: NominalCommand,
cmd: delegationtest.NominalCommand,
pol: policy.Policy{},
opts: []delegation.Option{
delegation.WithSubject(didtest.PersonaAlice.DID()),
@@ -107,10 +109,10 @@ func (g *generator) chainPersonas(personas []didtest.Persona, acc acc, vari vari
if personas[0] == didtest.PersonaCarol {
variants := []variant{
{name: "InvalidExpandedCommand", variant: func(p *newDelegationParams) {
p.cmd = ExpandedCommand
p.cmd = delegationtest.ExpandedCommand
}},
{name: "ValidAttenuatedCommand", variant: func(p *newDelegationParams) {
p.cmd = AttenuatedCommand
p.cmd = delegationtest.AttenuatedCommand
}},
{name: "InvalidSubject", variant: func(p *newDelegationParams) {
p.opts = append(p.opts, delegation.WithSubject(didtest.PersonaBob.DID()))
@@ -164,7 +166,7 @@ func (g *generator) createDelegation(params newDelegationParams, name string, va
dlgName += "_" + vari.name
}
err = os.WriteFile(filepath.Join(tokenDir, dlgName+tokenExt), data, 0o644)
err = os.WriteFile(filepath.Join("..", delegationtest.TokenDir, dlgName+tokenExt), data, 0o644)
if err != nil {
return cid.Undef, err
}
@@ -223,5 +225,5 @@ func (g *generator) writeGoFile() error {
file.Line()
}
return file.Save("token_gen.go")
return file.Save("../token_gen.go")
}

View File

@@ -0,0 +1,17 @@
package main
import (
"github.com/ucan-wg/go-ucan/did/didtest"
)
func main() {
gen := &generator{}
err := gen.chainPersonas(didtest.Personas(), acc{}, noopVariant())
if err != nil {
panic(err)
}
err = gen.writeGoFile()
if err != nil {
panic(err)
}
}

View File

@@ -1,26 +0,0 @@
package delegationtest
import (
"testing"
"github.com/stretchr/testify/require"
"gotest.tools/v3/golden"
"github.com/ucan-wg/go-ucan/did/didtest"
)
// TestUpdate doesn't actually run a test but uses the Go testing library
// to trigger generation of the delegation tokens and associated Go file.
func TestUpdate(t *testing.T) {
if golden.FlagUpdate() {
update(t)
}
}
func update(t *testing.T) {
t.Helper()
gen := &generator{}
require.NoError(t, gen.chainPersonas(didtest.Personas(), acc{}, noopVariant()))
require.NoError(t, gen.writeGoFile())
}

View File

@@ -11,11 +11,6 @@ import (
"github.com/ucan-wg/go-ucan/token/delegation"
)
const (
tokenDir = "data"
tokenExt = ".dagcbor"
)
var (
// ExpandedCommand is the parent of the NominalCommand and represents
// the cases where the delegation proof-chain or invocation token tries
@@ -35,6 +30,8 @@ var (
// ProofEmpty provides an empty proof chain for testing purposes.
var ProofEmpty = []cid.Cid{}
const TokenDir = "data"
//go:embed data
var fs embed.FS
@@ -74,7 +71,7 @@ func (l *delegationLoader) GetDelegation(id cid.Cid) (*delegation.Token, error)
}
func loadDelegations() (delegation.Loader, error) {
dirEntries, err := fs.ReadDir("data")
dirEntries, err := fs.ReadDir(TokenDir)
if err != nil {
return nil, err
}
@@ -82,7 +79,7 @@ func loadDelegations() (delegation.Loader, error) {
tkns := make(map[cid.Cid]*delegation.Token, len(dirEntries))
for _, dirEntry := range dirEntries {
data, err := fs.ReadFile(filepath.Join(tokenDir, dirEntry.Name()))
data, err := fs.ReadFile(filepath.Join(TokenDir, dirEntry.Name()))
if err != nil {
return nil, err
}