Skip to content

Commit

Permalink
add openLogger and openFS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Feb 3, 2023
1 parent dcac37e commit c194d27
Showing 1 changed file with 78 additions and 3 deletions.
81 changes: 78 additions & 3 deletions slackdump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"context"
"log"
"math"
"os"
"path/filepath"
"reflect"
"testing"
"time"

"github.com/slack-go/slack"
"github.com/stretchr/testify/assert"

"github.com/rusq/slackdump/v2/auth"
"github.com/rusq/slackdump/v2/internal/fixtures"
"github.com/rusq/slackdump/v2/internal/network"
"github.com/rusq/slackdump/v2/logger"
"github.com/rusq/slackdump/v2/types"
"github.com/slack-go/slack"
"github.com/stretchr/testify/assert"
)

func Test_newLimiter(t *testing.T) {
Expand Down Expand Up @@ -171,3 +173,76 @@ func TestSession_Me(t *testing.T) {
})
}
}

func TestSession_openFS(t *testing.T) {
t.Run("ensure that fs os open and close function added", func(t *testing.T) {
var sd = new(Session)
dir := t.TempDir()
sd.cfg = Config{}
testFile := filepath.Join(dir, "test.zip")

assert.NoError(t, sd.openFS(testFile))
assert.NotNil(t, sd.fs)
assert.Len(t, sd.atClose, 1)
assert.NoError(t, sd.Close())
assert.FileExists(t, testFile)
})
t.Run("ensure works with directory", func(t *testing.T) {
var sd = new(Session)
dir := t.TempDir()
sd.cfg = Config{}
testDir := filepath.Join(dir, "test")

assert.NoError(t, sd.openFS(testDir))
assert.NotNil(t, sd.fs)
assert.Len(t, sd.atClose, 1)

assert.NoError(t, sd.fs.WriteFile("test.txt", []byte("test"), 0644))

assert.NoError(t, sd.Close())
assert.DirExists(t, testDir)
})
}

func TestSession_openLogger(t *testing.T) {
t.Run("empty filename should log to stderr", func(t *testing.T) {
var sd = new(Session)
sd.cfg = Config{}
assert.NoError(t, sd.openLogger(""))
assert.NotNil(t, sd.log)
assert.Equal(t, sd.log, logger.Default)
assert.Len(t, sd.atClose, 0) // no close function for stderr
assert.NoError(t, sd.Close())
})
t.Run("non-empty file creates a log file", func(t *testing.T) {
testLogFile(t, filepath.Join(t.TempDir(), "test.log"), "hello log")
})
t.Run("new data is appended to log file if it exists", func(t *testing.T) {
testFile := filepath.Join(t.TempDir(), "test_again.log")
testLogFile(t, testFile, "hello log")
testLogFile(t, testFile, "hello again log")

data, err := os.ReadFile(testFile)
assert.NoError(t, err)
assert.Contains(t, string(data), "hello log")
assert.Contains(t, string(data), "hello again log")
})
}

func testLogFile(t *testing.T, testFile string, msg string) {
var sd = new(Session)
sd.cfg = Config{}

assert.NoError(t, sd.openLogger(testFile))
assert.NotNil(t, sd.log)
assert.Len(t, sd.atClose, 1)

sd.log.Print(msg)

assert.NoError(t, sd.Close())
assert.FileExists(t, testFile)

data, err := os.ReadFile(testFile)
assert.NoError(t, err)
assert.Contains(t, string(data), msg)
}

0 comments on commit c194d27

Please sign in to comment.