github.com/qlik-oss/gopherciser@v0.18.6/logger/filewriter_test.go (about)

     1  package logger
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  // setTmpWorkDir changes the current working directory to the temp directory
    12  // based on the OS. Hence the log files (and possibly folders) are created there
    13  func setTmpWorkDir() error {
    14  	if err := os.Chdir(os.TempDir()); err != nil {
    15  		return err
    16  	}
    17  	return nil
    18  }
    19  
    20  func TestNewWriter(t *testing.T) {
    21  	err := setTmpWorkDir()
    22  	assert.NoError(t, err)
    23  	file := "some_file"
    24  	w, err := NewWriter(file)
    25  	assert.NotNil(t, w)
    26  	assert.NoError(t, err)
    27  	_, statErr := os.Stat(file)
    28  	assert.NoError(t, statErr)
    29  	os.Remove(file)
    30  }
    31  
    32  func TestNewWriter_withFolder(t *testing.T) {
    33  	err := setTmpWorkDir()
    34  	assert.NoError(t, err)
    35  	dir := "some_fancy_dir"
    36  	file := "some_file"
    37  	filePath := filepath.Join(dir, file)
    38  	w, err := NewWriter(filePath)
    39  	assert.NotNil(t, w)
    40  	assert.NoError(t, err)
    41  	_, statErr := os.Stat(filePath)
    42  	assert.NoError(t, statErr)
    43  	os.RemoveAll(dir)
    44  }