test: add config to tests

This commit is contained in:
Slug-Boi
2025-06-03 17:29:25 +02:00
parent 45d4c6e8b4
commit bbce5d3fab
2 changed files with 39 additions and 4 deletions
+20 -4
View File
@@ -38,7 +38,14 @@ const author_data = `
} }
}` }`
const config_data = `[settings]
author_file = "author_file_test"
starting_scope = "git"
editor = "built-in"
`
var envVar string var envVar string
var configVar string
func setup() { func setup() {
// setup test data // setup test data
@@ -49,6 +56,14 @@ func setup() {
os.Setenv("author_file", "author_file_test") os.Setenv("author_file", "author_file_test")
envVar = os.Getenv("author_file") envVar = os.Getenv("author_file")
err = os.WriteFile("test_config.toml", []byte(config_data), 0644)
if err != nil {
panic(err)
}
os.Setenv("COCOMMIT_CONFIG", "test_config.toml")
configVar = os.Getenv("COCOMMIT_CONFIG")
utils.Define_users("author_file_test") utils.Define_users("author_file_test")
} }
@@ -56,6 +71,7 @@ func teardown() {
// remove test data // remove test data
os.Remove("author_file_test") os.Remove("author_file_test")
os.Setenv("author_file", envVar) os.Setenv("author_file", envVar)
os.Remove("test_config.toml")
} }
func keyPress(tm *teatest.TestModel, key string) { func keyPress(tm *teatest.TestModel, key string) {
@@ -610,8 +626,8 @@ func Test_ScopesLocal(t *testing.T) {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }
if m.scope!= local_scope { if m.scope!= mixed_scope {
t.Errorf("Expected scope to be %v, got %v", local_scope, m.scope) t.Errorf("Expected scope to be %v, got %v", mixed_scope, m.scope)
} }
} }
@@ -634,8 +650,8 @@ func Test_ScopesMixed(t *testing.T) {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }
if m.scope != mixed_scope { if m.scope != local_scope {
t.Errorf("Expected scope to be %v, got %v", mixed_scope, m.scope) t.Errorf("Expected scope to be %v, got %v", local_scope, m.scope)
} }
} }
+19
View File
@@ -38,10 +38,25 @@ const author_data = `
} }
}` }`
const config_data = `[settings]
author_file = "author_file_test"
starting_scope = "git"
editor = "built-in"
`
var envVar = os.Getenv("author_file") var envVar = os.Getenv("author_file")
func setup() { func setup() {
// setup test data // setup test data
err := os.WriteFile("test_config.toml", []byte(config_data), 0644)
if err != nil {
panic(err)
}
os.Setenv("COCOMMIT_CONFIG", "test_config.toml")
utils.Find_authorfile()
os.WriteFile("author_file_test", []byte(author_data), 0644) os.WriteFile("author_file_test", []byte(author_data), 0644)
os.Setenv("author_file", "author_file_test") os.Setenv("author_file", "author_file_test")
} }
@@ -50,6 +65,7 @@ func teardown() {
// remove test data // remove test data
os.Remove("author_file_test") os.Remove("author_file_test")
os.Setenv("author_file", envVar) os.Setenv("author_file", envVar)
os.Remove("test_config.toml")
} }
// Author tests BEGIN // Author tests BEGIN
@@ -128,10 +144,12 @@ func Test_FindAuthorFilePanic(t *testing.T) {
originalAuthorFile := os.Getenv("author_file") originalAuthorFile := os.Getenv("author_file")
originalHome := os.Getenv("HOME") originalHome := os.Getenv("HOME")
orignalXDG := os.Getenv("XDG_CONFIG_HOME") orignalXDG := os.Getenv("XDG_CONFIG_HOME")
originalAuthorFile_Config := utils.ConfigVar.Settings.AuthorFile
// Test Find_authorfile panic // Test Find_authorfile panic
defer func() { defer func() {
// Reset environment variables // Reset environment variables
utils.ConfigVar.Settings.AuthorFile = originalAuthorFile_Config
os.Setenv("author_file", originalAuthorFile) os.Setenv("author_file", originalAuthorFile)
os.Setenv("HOME", originalHome) os.Setenv("HOME", originalHome)
os.Setenv("XDG_CONFIG_HOME", orignalXDG) os.Setenv("XDG_CONFIG_HOME", orignalXDG)
@@ -143,6 +161,7 @@ func Test_FindAuthorFilePanic(t *testing.T) {
// Set environment variables to empty strings // Set environment variables to empty strings
// to trigger the panic // to trigger the panic
utils.ConfigVar.Settings.AuthorFile = ""
os.Setenv("author_file", "") os.Setenv("author_file", "")
os.Setenv("HOME", "") os.Setenv("HOME", "")
os.Setenv("XDG_CONFIG_HOME", "") os.Setenv("XDG_CONFIG_HOME", "")