fix workspace tests

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-05-25 13:03:44 -07:00
parent bfe9097f66
commit 1e37a0862c
3 changed files with 64 additions and 43 deletions

View File

@ -1,39 +0,0 @@
package state
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestStore(t *testing.T) {
ctx := context.TODO()
// Init
root, err := os.MkdirTemp(os.TempDir(), "dagger-*")
require.NoError(t, err)
st, err := Init(ctx, root, "test")
require.Equal(t, "test", st.Name)
require.Equal(t, root, st.Path)
require.NoError(t, err)
// Open
_, err = Open(ctx, "/tmp/not/exist")
require.Error(t, err)
require.ErrorIs(t, ErrNotInit, err)
st, err = Open(ctx, root)
require.NoError(t, err)
require.Equal(t, "test", st.Name)
require.Equal(t, root, st.Path)
// Save
computed := `{"hello": "world"}`
st.Computed = computed
require.NoError(t, Save(ctx, st))
st, err = Open(ctx, root)
require.NoError(t, err)
require.Equal(t, computed, st.Computed)
}

View File

@ -35,14 +35,19 @@ type Workspace struct {
} }
func Init(ctx context.Context, dir string) (*Workspace, error) { func Init(ctx context.Context, dir string) (*Workspace, error) {
root := path.Join(dir, daggerDir) root, err := filepath.Abs(dir)
if err := os.Mkdir(root, 0755); err != nil { if err != nil {
return nil, err
}
daggerRoot := path.Join(root, daggerDir)
if err := os.Mkdir(daggerRoot, 0755); err != nil {
if errors.Is(err, os.ErrExist) { if errors.Is(err, os.ErrExist) {
return nil, ErrAlreadyInit return nil, ErrAlreadyInit
} }
return nil, err return nil, err
} }
if err := os.Mkdir(path.Join(root, envDir), 0755); err != nil { if err := os.Mkdir(path.Join(daggerRoot, envDir), 0755); err != nil {
return nil, err return nil, err
} }
return &Workspace{ return &Workspace{
@ -178,7 +183,7 @@ func (w *Workspace) Save(ctx context.Context, st *State) error {
} }
// Only update the encrypted file if there were changes // Only update the encrypted file if there were changes
if bytes.Compare(data, currentPlain) != 0 { if !bytes.Equal(data, currentPlain) {
encrypted, err := keychain.Reencrypt(ctx, manifestPath, data) encrypted, err := keychain.Reencrypt(ctx, manifestPath, data)
if err != nil { if err != nil {
return err return err

View File

@ -0,0 +1,55 @@
package state
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestWorkspace(t *testing.T) {
ctx := context.TODO()
root, err := os.MkdirTemp(os.TempDir(), "dagger-*")
require.NoError(t, err)
// Open should fail since the directory is not initialized
_, err = Open(ctx, root)
require.ErrorIs(t, ErrNotInit, err)
// Init
workspace, err := Init(ctx, root)
require.NoError(t, err)
require.Equal(t, root, workspace.Path)
// Create
st, err := workspace.Create(ctx, "test")
require.NoError(t, err)
require.Equal(t, "test", st.Name)
// Open
workspace, err = Open(ctx, root)
require.NoError(t, err)
require.Equal(t, root, workspace.Path)
// List
envs, err := workspace.List(ctx)
require.NoError(t, err)
require.Len(t, envs, 1)
require.Equal(t, "test", envs[0].Name)
// Get
env, err := workspace.Get(ctx, "test")
require.NoError(t, err)
require.Equal(t, "test", env.Name)
// Save
require.NoError(t, env.SetInput("foo", TextInput("bar")))
require.NoError(t, workspace.Save(ctx, env))
workspace, err = Open(ctx, root)
require.NoError(t, err)
env, err = workspace.Get(ctx, "test")
require.NoError(t, err)
require.Contains(t, env.Inputs, "foo")
}