-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bac2ff3
commit 53c4b22
Showing
29 changed files
with
435 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package access | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestGoodFromContext(t *testing.T) { | ||
ctx := WithContext(context.TODO(), AdminAccess) | ||
if ac := FromContext(ctx); ac != AdminAccess { | ||
t.Errorf("FromContext(ctx) => %d, want %d", ac, AdminAccess) | ||
} | ||
} | ||
|
||
func TestBadFromContext(t *testing.T) { | ||
ctx := context.TODO() | ||
if ac := FromContext(ctx); ac != -1 { | ||
t.Errorf("FromContext(ctx) => %d, want %d", ac, -1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestBadFromContext(t *testing.T) { | ||
ctx := context.TODO() | ||
if c := FromContext(ctx); c != nil { | ||
t.Errorf("FromContext(ctx) => %v, want %v", c, nil) | ||
} | ||
} | ||
|
||
func TestGoodFromContext(t *testing.T) { | ||
ctx := WithContext(context.TODO(), &Config{}) | ||
if c := FromContext(ctx); c == nil { | ||
t.Errorf("FromContext(ctx) => %v, want %v", c, &Config{}) | ||
} | ||
} | ||
|
||
func TestGoodFromContextWithDefaultConfig(t *testing.T) { | ||
cfg := DefaultConfig() | ||
ctx := WithContext(context.TODO(), cfg) | ||
if c := FromContext(ctx); c == nil || !reflect.DeepEqual(c, cfg) { | ||
t.Errorf("FromContext(ctx) => %v, want %v", c, cfg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package config | ||
|
||
import "testing" | ||
|
||
func TestNewConfigFile(t *testing.T) { | ||
for _, cfg := range []*Config{ | ||
nil, | ||
DefaultConfig(), | ||
&Config{}, | ||
} { | ||
if s := newConfigFile(cfg); s == "" { | ||
t.Errorf("newConfigFile(nil) => %q, want non-empty string", s) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,33 @@ | ||
package config | ||
|
||
import "github.com/charmbracelet/keygen" | ||
import ( | ||
"errors" | ||
|
||
"github.com/charmbracelet/keygen" | ||
) | ||
|
||
var ( | ||
// ErrNilConfig is returned when a nil config is passed to a function. | ||
ErrNilConfig = errors.New("nil config") | ||
|
||
// ErrEmptySSHKeyPath is returned when the SSH key path is empty. | ||
ErrEmptySSHKeyPath = errors.New("empty SSH key path") | ||
) | ||
|
||
// KeyPair returns the server's SSH key pair. | ||
func (c SSHConfig) KeyPair() (*keygen.SSHKeyPair, error) { | ||
return keygen.New(c.KeyPath, keygen.WithKeyType(keygen.Ed25519)) | ||
} | ||
|
||
// KeyPair returns the server's SSH key pair. | ||
func KeyPair(cfg *Config) (*keygen.SSHKeyPair, error) { | ||
if cfg == nil { | ||
return nil, ErrNilConfig | ||
} | ||
|
||
if cfg.SSH.KeyPath == "" { | ||
return nil, ErrEmptySSHKeyPath | ||
} | ||
|
||
return keygen.New(cfg.SSH.KeyPath, keygen.WithKeyType(keygen.Ed25519)) | ||
Check failure on line 32 in pkg/config/ssh.go GitHub Actions / lint-soft
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package config | ||
|
||
import "testing" | ||
|
||
func TestBadSSHKeyPair(t *testing.T) { | ||
for _, cfg := range []*Config{ | ||
nil, | ||
{}, | ||
} { | ||
if _, err := KeyPair(cfg); err == nil { | ||
t.Errorf("cfg.SSH.KeyPair() => _, nil, want non-nil error") | ||
} | ||
} | ||
} | ||
|
||
func TestGoodSSHKeyPair(t *testing.T) { | ||
cfg := &Config{ | ||
SSH: SSHConfig{ | ||
KeyPath: "testdata/ssh_host_ed25519_key", | ||
}, | ||
} | ||
|
||
if _, err := KeyPair(cfg); err != nil { | ||
t.Errorf("cfg.SSH.KeyPair() => _, %v, want nil error", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cron | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/charmbracelet/log" | ||
) | ||
|
||
func TestCronLogger(t *testing.T) { | ||
var buf bytes.Buffer | ||
logger := log.New(&buf) | ||
logger.SetLevel(log.DebugLevel) | ||
clogger := cronLogger{logger} | ||
clogger.Info("foo") | ||
clogger.Error(fmt.Errorf("bar"), "test") | ||
if buf.String() != "DEBU foo\nERRO test err=bar\n" { | ||
t.Errorf("unexpected log output: %s", buf.String()) | ||
} | ||
} | ||
|
||
func TestSchedularAddRemove(t *testing.T) { | ||
s := NewScheduler(context.TODO()) | ||
id, err := s.AddFunc("* * * * *", func() {}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
s.Remove(id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package db_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/charmbracelet/soft-serve/pkg/db" | ||
"github.com/charmbracelet/soft-serve/pkg/db/internal/test" | ||
) | ||
|
||
func TestBadFromContext(t *testing.T) { | ||
ctx := context.TODO() | ||
if c := db.FromContext(ctx); c != nil { | ||
t.Errorf("FromContext(ctx) => %v, want %v", c, nil) | ||
} | ||
} | ||
|
||
func TestGoodFromContext(t *testing.T) { | ||
ctx := context.TODO() | ||
dbx, err := test.OpenSqlite(ctx, t) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
ctx = db.WithContext(ctx, dbx) | ||
if c := db.FromContext(ctx); c == nil { | ||
t.Errorf("FromContext(ctx) => %v, want %v", c, dbx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestOpenUnknownDriver(t *testing.T) { | ||
_, err := Open(context.TODO(), "invalid", "") | ||
if err == nil { | ||
t.Error("Open(invalid) => nil, want error") | ||
} | ||
if !strings.Contains(err.Error(), "unknown driver") { | ||
t.Errorf("Open(invalid) => %v, want error containing 'unknown driver'", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package db | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestWrapErrorBadNoRows(t *testing.T) { | ||
for _, e := range []error{ | ||
fmt.Errorf("foo"), | ||
errors.New("bar"), | ||
} { | ||
if err := WrapError(e); err != e { | ||
t.Errorf("WrapError(%v) => %v, want %v", e, err, e) | ||
} | ||
} | ||
} | ||
|
||
func TestWrapErrorGoodNoRows(t *testing.T) { | ||
if err := WrapError(sql.ErrNoRows); err != ErrRecordNotFound { | ||
t.Errorf("WrapError(sql.ErrNoRows) => %v, want %v", err, ErrRecordNotFound) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/charmbracelet/soft-serve/pkg/db" | ||
) | ||
|
||
// OpenSqlite opens a new temp SQLite database for testing. | ||
// It removes the database file when the test is done using tb.Cleanup. | ||
// If ctx is nil, context.TODO() is used. | ||
func OpenSqlite(ctx context.Context, tb testing.TB) (*db.DB, error) { | ||
if ctx == nil { | ||
ctx = context.TODO() | ||
} | ||
dbpath := filepath.Join(tb.TempDir(), "test.db") | ||
dbx, err := db.Open(ctx, "sqlite", dbpath) | ||
if err != nil { | ||
return nil, err | ||
Check failure on line 21 in pkg/db/internal/test/test.go GitHub Actions / lint-soft
|
||
} | ||
tb.Cleanup(func() { | ||
if err := dbx.Close(); err != nil { | ||
tb.Error(err) | ||
} | ||
}) | ||
return dbx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package migrate | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/charmbracelet/soft-serve/pkg/config" | ||
"github.com/charmbracelet/soft-serve/pkg/db/internal/test" | ||
) | ||
|
||
func TestMigrate(t *testing.T) { | ||
// XXX: we need a config.Config in the context for the migrations to run | ||
// properly. Some migrations depend on the config being present. | ||
ctx := config.WithContext(context.TODO(), config.DefaultConfig()) | ||
dbx, err := test.OpenSqlite(ctx, t) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := Migrate(ctx, dbx); err != nil { | ||
t.Errorf("Migrate() => %v, want nil error", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.