Skip to content
Snippets Groups Projects
Select Git revision
  • aa0ccb8a253386e276cac360426c039460650e04
  • master default protected
  • v1.22.9
  • v1.22.8
  • v1.22.7
  • v1.22.6
  • v1.22.5
  • v1.22.4
  • v1.22.3
  • v1.22.1
  • v1.22.0
  • v1.21.0
  • v1.20.5
  • v1.20.4
  • v1.20.3
  • v1.20.2
  • v1.20.1
  • v1.20.0
  • v1.19.0
  • v1.18.3
  • v1.18.2
  • v1.18.1
22 results

file_test.go

Blame
  • file_test.go 9.61 KiB
    // Copyright 2022 schukai GmbH
    // SPDX-License-Identifier: AGPL-3.0
    
    package configuration
    
    import (
    	"bytes"
    	"github.com/stretchr/testify/assert"
    	"io"
    	"io/fs"
    	"os"
    	"testing"
    	"time"
    )
    
    type mockFS struct{}
    
    type mockFile struct {
    	fs.FileInfo
    	buffer io.Reader
    }
    
    type mockFileInfo struct {
    	name string
    	size int64
    	mode fs.FileMode
    	mod  time.Time
    	dir  bool
    	sys  interface{}
    }
    
    func (m mockFileInfo) Name() string {
    	return m.name
    }
    
    func (m mockFileInfo) Size() int64 {
    	return m.size
    }
    
    func (m mockFileInfo) Mode() fs.FileMode {
    	return m.mode
    }
    
    func (m mockFileInfo) ModTime() time.Time {
    	return m.mod
    }
    
    func (m mockFileInfo) IsDir() bool {
    	return m.dir
    }
    
    func (m mockFileInfo) Sys() any {
    	return m.sys
    }
    
    func (f mockFile) Stat() (fs.FileInfo, error) {
    	fi := mockFileInfo{}
    	return fi, nil
    }
    
    func (f mockFile) Read(b []byte) (int, error) {
    	return f.buffer.Read(b)
    }
    
    func (f mockFile) Close() error {
    	return nil
    }
    
    type fileSample struct {
    	file *mockFile
    	err  error
    }
    
    var fileSamples []fileSample
    
    func (mockFS) Open(name string) (fs.File, error) {
    
    	if len(fileSamples) > 0 {
    
    		s := fileSamples[0]
    		fileSamples = fileSamples[1:]
    
    		if s.err != nil {
    			return nil, s.err
    		}
    
    		return s.file, nil
    
    	}
    
    	return os.Open(name)
    }
    
    func TestDirectories(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    	c := New(cfg)
    	c.AddDirectory("/a")
    
    	if c.Directories() == nil {
    		t.Error("Expected not nil")
    	}
    
    	if len(c.Directories()) != 1 {
    		t.Error("Expected 1")
    	}
    
    }
    
    func TestSetDirectories(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(&cfg)
    	c.SetDirectories([]string{"/a", "/b"})
    
    	if c.Directories() == nil {
    		t.Error("Expected not nil")
    	}
    
    	if len(c.Directories()) != 2 {
    		t.Error("Expected 2")
    	}
    
    }
    
    func TestAddWorkingDirectory(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(&cfg)
    	c.AddWorkingDirectory()
    
    	if c.Directories() == nil {
    		t.Error("Expected not nil")
    	}
    
    	if len(c.Directories()) != 1 {
    		t.Error("Expected 1")
    	}
    
    }
    
    func TestAddWorkingDirectoryTwice(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(&cfg)
    	c.AddWorkingDirectory()
    	c.AddWorkingDirectory()
    
    	if c.Directories() == nil {
    		t.Error("Expected not nil")
    	}
    
    	if len(c.Directories()) != 2 {
    		t.Error("Expected 2 got ", len(c.Directories()))
    	}
    
    	if c.Errors() == nil {
    		t.Error("Expected not nil")
    	}
    
    }
    
    func TestAddDirectoryMissingMnemonic(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(cfg)
    	c.AddEtcDirectory()
    
    	if !c.HasErrors() {
    		t.Error("Expected error")
    	}
    
    	if c.Errors()[0] != MnemonicEmptyError {
    		t.Error("Expected error")
    	}
    
    }
    func TestAddDirectory(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(&cfg)
    	c.SetMnemonic("test")
    	c.AddEtcDirectory()
    
    	if c.Directories() == nil {
    		t.Error("Expected not nil")
    	}
    
    	if len(c.Directories()) != 1 {
    		t.Error("Expected 1")
    	}
    
    }
    
    func TestAddDirectoryTwice(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(&cfg)
    	c.SetMnemonic("test")
    	c.AddEtcDirectory()
    	c.AddEtcDirectory()
    
    	if c.Directories() == nil {
    		t.Error("Expected not nil")
    	}
    
    	if len(c.Directories()) != 2 {
    		t.Error("Expected 1 got ", len(c.Directories()))
    	}
    
    	if c.Errors() == nil {
    		t.Error("Expected not nil")
    	}
    
    }
    
    func TestAddUserDirectory(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(&cfg)
    	c.SetMnemonic("test")
    	c.AddUserConfigDirectory()
    
    	if c.Directories() == nil {
    		t.Error("Expected not nil")
    	}
    
    	if len(c.Directories()) != 1 {
    		t.Error("Expected 1")
    	}
    
    }
    
    func TestSetDefaultPath(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(&cfg)
    	c.SetMnemonic("test")
    	c.SetDefaultDirectories()
    
    	if c.Directories() == nil {
    		t.Error("Expected not nil")
    	}
    
    	if len(c.Directories()) != 3 {
    		t.Error("Expected 3 got ", len(c.Directories()))
    	}
    
    }
    
    func TestSetFileFormat(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(cfg)
    	c.SetFileFormat(Yaml)
    
    	if c.HasErrors() {
    		t.Error("Expected not error")
    	}
    
    }
    
    func TestSetFileFormatError(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(cfg)
    	c.SetFileFormat(Format(9999))
    
    	if !c.HasErrors() {
    		t.Error("Expected error")
    	}
    
    }
    
    func TestImport(t *testing.T) {
    
    	fileSamples = []fileSample{}
    	defer func() { fileSamples = nil }()
    
    	addSample1()
    
    	defaults := ConfigStruct2{
    		A: "Hello!",
    		D: map[string]string{"A": "1", "B": "2"},
    		F: 99,
    	}
    
    	mockFs := mockFS{}
    
    	c := New(defaults)
    	c.SetMnemonic("test")
    	c.SetFileFormat(Yaml)
    	c.SetFilesystem(mockFs)
    	// setDefaultdirectories can be returned errors
    	c.SetDefaultDirectories().ResetErrors()
    	c.Import()
    
    	if c.HasErrors() {
    		t.Error("Expected not error", c.Errors())
    	}
    
    	if c.Config().A != "from file" {
    		t.Error("Expected \"from file\" got ", c.Config().A)
    	}
    
    	m := c.Config().D.(map[string]interface{})
    
    	if m["A"] != "x" {
    		t.Error("Expected x got ", m["A"])
    	}
    
    	if m["B"] != "2" {
    		t.Error("Expected 2 got ", m["B"])
    	}
    
    }
    
    func addSample1() {
    
    	e := &mockFile{}
    
    	content := []byte(
    		`---
    A: "from file"
    F: 3
    D: 
      A: x
    C:
      - CA: 
          CAA: 1
      - CB: 2
    
    ...
    
    `)
    
    	e.buffer = bytes.NewBuffer(content)
    
    	e.FileInfo = mockFileInfo{
    		name: "test",
    		size: int64(len(content)),
    		mode: 0,
    		mod:  time.Now(),
    		dir:  false,
    		sys:  nil,
    	}
    
    	fileSamples = append(fileSamples, fileSample{
    		file: e,
    	})
    
    }
    func addSample2() {
    
    	e := &mockFile{}
    
    	content := []byte(
    		`---
    F: 5
    C:
      - CA: 
          CAA: 1
      - CB: 2
    ...
    
    `)
    
    	e.buffer = bytes.NewBuffer(content)
    
    	e.FileInfo = mockFileInfo{
    		name: "test",
    		size: int64(len(content)),
    		mode: 0,
    		mod:  time.Now(),
    		dir:  false,
    		sys:  nil,
    	}
    
    	fileSamples = append(fileSamples, fileSample{
    		file: e,
    	})
    
    }
    
    // Import two files. The second file should overwrite the first file.
    func TestImport2(t *testing.T) {
    
    	fileSamples = []fileSample{}
    	defer func() { fileSamples = nil }()
    
    	addSample2()
    	addSample1()
    
    	defaults := ConfigStruct2{
    		A: "Hello!",
    		D: map[string]string{"A": "1", "B": "2"},
    		F: 99,
    	}
    
    	mockFs := mockFS{}
    
    	c := New(defaults)
    	c.SetMnemonic("test")
    	c.SetFileFormat(Yaml)
    	c.SetFilesystem(mockFs)
    	c.SetDefaultDirectories().ResetErrors() // errors not important here
    	c.Import()
    
    	if c.HasErrors() {
    		t.Error("Expected not error", c.Errors())
    	}
    
    	if c.Config().A != "from file" {
    		t.Error("Expected \"from file\" got ", c.Config().A)
    	}
    
    	m := c.Config().D.(map[string]interface{})
    
    	if m["A"] != "x" {
    		t.Error("Expected x got ", m["A"])
    	}
    
    	if m["B"] != "2" {
    		t.Error("Expected 2 got ", m["B"])
    	}
    
    	if c.Config().F != 5 {
    		t.Error("Expected 5 got ", c.Config().F)
    	}
    
    }
    
    // Import two files. The second file should overwrite the first file.
    func TestImport3(t *testing.T) {
    
    	fileSamples = []fileSample{}
    	defer func() { fileSamples = nil }()
    
    	addSample1()
    	addSample2()
    
    	defaults := ConfigStruct2{
    		A: "Hello!",
    		D: map[string]string{"A": "1", "B": "2"},
    		F: 99,
    	}
    
    	mockFs := mockFS{}
    
    	c := New(defaults)
    	c.SetMnemonic("test")
    	c.SetFileFormat(Yaml)
    	c.SetFilesystem(mockFs)
    	c.SetDefaultDirectories().ResetErrors() // errors not important here
    	c.Import()
    
    	if c.HasErrors() {
    		t.Error("Expected not error", c.Errors())
    	}
    
    	if c.Config().A != "from file" {
    		t.Error("Expected \"from file\" got ", c.Config().A)
    	}
    
    	m := c.Config().D.(map[string]interface{})
    
    	if m["A"] != "x" {
    		t.Error("Expected x got ", m["A"])
    	}
    
    	if m["B"] != "2" {
    		t.Error("Expected 2 got ", m["B"])
    	}
    
    	if c.Config().F != 3 {
    		t.Error("Expected 3 got ", c.Config().F)
    	}
    
    }
    
    func TestSettingAddFile(t *testing.T) {
    	cfg := ConfigStruct2{}
    
    	f, err := os.CreateTemp("", "watch_test")
    	if err != nil {
    		t.Error(err)
    		return
    	}
    
    	defer os.Remove(f.Name())
    	f.WriteString("A: \"from file\"")
    
    	c := New(cfg)
    	c.AddFile(f.Name(), Yaml)
    
    	assert.Equal(t, c.Config().A, "")
    
    	c.Import()
    	assert.Equal(t, c.Config().A, "from file")
    
    }
    
    func TestRemoveFile(t *testing.T) {
    	cfg := ConfigStruct2{}
    
    	f, err := os.CreateTemp("", "watch_test")
    	if err != nil {
    		t.Error(err)
    		return
    	}
    
    	defer os.Remove(f.Name())
    
    	f.WriteString("A: \"from file\"")
    
    	c := New(cfg)
    	c.AddFile(f.Name(), Yaml)
    	assert.Equal(t, c.Config().A, "")
    
    	c.Import()
    	assert.Equal(t, c.Config().A, "from file")
    
    	assert.True(t, c.HasFile(f.Name()))
    	c.RemoveFile(f.Name())
    	assert.False(t, c.HasFile(f.Name()))
    
    }
    
    func TestSettingAddFileWithRecognise(t *testing.T) {
    	cfg := ConfigStruct2{}
    
    	f, err := os.CreateTemp("", "watch_test")
    	if err != nil {
    		t.Error(err)
    		return
    	}
    
    	defer os.Remove(f.Name())
    	f.WriteString("A: \"from file\"")
    
    	c := New(cfg)
    	c.AddFile(f.Name(), RecogniseFormat)
    
    	assert.Equal(t, c.Config().A, "")
    
    	c.Import()
    	assert.Equal(t, c.Config().A, "from file")
    
    }
    
    func TestSetConfigName(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(cfg)
    	c.SetFileName("test")
    
    	if c.HasErrors() {
    		t.Error("Expected not error")
    	}
    
    }
    
    func TestConfigSetFileName(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(cfg)
    	c.SetFileName("test")
    
    	if c.HasErrors() {
    		t.Error("Expected not error")
    	}
    
    	c.SetFileName("")
    
    	if !c.HasErrors() {
    		t.Error("Expected error")
    	}
    
    }
    
    func TestConfigSetFileFormat(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(cfg)
    	c.SetFileFormat(99)
    
    	if !c.HasErrors() {
    		t.Error("Expected not error")
    	}
    
    }
    
    func TestConfigSetDirectories(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(cfg)
    	c.SetDirectories([]string{"a", "b"})
    
    	if !c.HasErrors() {
    		t.Error("Expected not error")
    	}
    
    }
    
    func TestConfigDirectories(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(cfg)
    	c.SetDirectories([]string{"a", "b"})
    
    	if !c.HasErrors() {
    		t.Error("Expected not error")
    	}
    
    	dirs := c.Directories()
    
    	if len(dirs) != 2 {
    		t.Error("Expected 2 got ", len(dirs))
    	}
    
    }
    
    func TestConfigAddDirectory(t *testing.T) {
    
    	cfg := ConfigStruct2{}
    
    	c := New(cfg)
    	c.AddDirectory("a")
    
    	if !c.HasErrors() {
    		t.Error("Expected not error")
    	}
    
    	dirs := c.Directories()
    
    	if len(dirs) != 1 {
    		t.Error("Expected 1 got ", len(dirs))
    	}
    
    }