-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a simple toml parser and cmd-line parser
- Loading branch information
1 parent
082e970
commit 4c878f0
Showing
7 changed files
with
316 additions
and
12 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
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,254 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"testing" | ||
|
||
"github.com/pelletier/go-toml/v2" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// Writes the contents into a temporary | ||
// toml file | ||
func writeToml(contents []byte) (s string, err error) { | ||
file, err := os.CreateTemp(os.TempDir(), "*.toml") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer func() { | ||
errors.Join(file.Close(), err) | ||
}() | ||
|
||
_, err = file.Write(contents) | ||
if err != nil { | ||
return "", err | ||
} | ||
return file.Name(), nil | ||
} | ||
|
||
func TestCorrectToml(t *testing.T) { | ||
expected := AppSettings{ | ||
DatabaseAddress: "test_database_address", | ||
DatabaseUser: "test_database_user", | ||
DatabasePassword: "test_database_password", | ||
DatabaseName: "test_database_name", | ||
WebserverPort: 99999, | ||
DatabasePort: 666, | ||
} | ||
bytes, err := toml.Marshal(expected) | ||
assert.Nil(t, err) | ||
|
||
filepath, err := writeToml(bytes) | ||
assert.Nil(t, err) | ||
|
||
actual, err := ReadConfigToml(filepath) | ||
assert.Nil(t, err) | ||
assert.Equal(t, actual, expected) | ||
} | ||
|
||
func TestMissingDatabaseAddress(t *testing.T) { | ||
|
||
missing_database_address := struct { | ||
DatabaseUser string `toml:"database_user"` | ||
DatabasePassword string `toml:"database_password"` | ||
DatabaseName string `toml:"database_name"` | ||
WebserverPort string `toml:"webserver_port"` | ||
DatabasePort string `toml:"database_port"` | ||
}{ | ||
DatabaseUser: "test_database_user", | ||
DatabasePassword: "test_database_password", | ||
DatabaseName: "test_database_name", | ||
WebserverPort: "99999", | ||
DatabasePort: "666", | ||
} | ||
|
||
bytes, err := toml.Marshal(missing_database_address) | ||
assert.Nil(t, err) | ||
|
||
filepath, err := writeToml(bytes) | ||
assert.Nil(t, err) | ||
|
||
_, err = ReadConfigToml(filepath) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestMissingDatabaseUser(t *testing.T) { | ||
|
||
missing_database_user := struct { | ||
DatabaseAddress string `toml:"database_address"` | ||
DatabasePassword string `toml:"database_password"` | ||
DatabaseName string `toml:"database_name"` | ||
WebserverPort string `toml:"webserver_port"` | ||
DatabasePort string `toml:"database_port"` | ||
}{ | ||
DatabaseAddress: "test_database_address", | ||
DatabasePassword: "test_database_password", | ||
DatabaseName: "test_database_name", | ||
WebserverPort: "99999", | ||
DatabasePort: "666", | ||
} | ||
|
||
bytes, err := toml.Marshal(missing_database_user) | ||
assert.Nil(t, err) | ||
|
||
filepath, err := writeToml(bytes) | ||
assert.Nil(t, err) | ||
|
||
_, err = ReadConfigToml(filepath) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestMissingDatabasePassword(t *testing.T) { | ||
missing_database_password := struct { | ||
DatabaseAddress string `toml:"database_address"` | ||
DatabaseUser string `toml:"database_user"` | ||
DatabaseName string `toml:"database_name"` | ||
WebserverPort string `toml:"webserver_port"` | ||
DatabasePort string `toml:"database_port"` | ||
}{ | ||
DatabaseAddress: "test_database_address", | ||
DatabaseUser: "test_database_user", | ||
DatabaseName: "test_database_name", | ||
WebserverPort: "99999", | ||
DatabasePort: "666", | ||
} | ||
|
||
bytes, err := toml.Marshal(missing_database_password) | ||
assert.Nil(t, err) | ||
|
||
filepath, err := writeToml(bytes) | ||
assert.Nil(t, err) | ||
|
||
_, err = ReadConfigToml(filepath) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestMissingDatabaseName(t *testing.T) { | ||
missing_database_name := struct { | ||
DatabaseAddress string `toml:"database_address"` | ||
DatabaseUser string `toml:"database_user"` | ||
DatabasePassword string `toml:"database_password"` | ||
WebserverPort string `toml:"webserver_port"` | ||
DatabasePort string `toml:"database_port"` | ||
}{ | ||
DatabaseAddress: "test_database_address", | ||
DatabaseUser: "test_database_user", | ||
DatabasePassword: "test_database_password", | ||
WebserverPort: "99999", | ||
DatabasePort: "666", | ||
} | ||
|
||
bytes, err := toml.Marshal(missing_database_name) | ||
assert.Nil(t, err) | ||
|
||
filepath, err := writeToml(bytes) | ||
assert.Nil(t, err) | ||
|
||
_, err = ReadConfigToml(filepath) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestMissingWebserverPort(t *testing.T) { | ||
missing_webserver_port := struct { | ||
DatabaseAddress string `toml:"database_address"` | ||
DatabaseUser string `toml:"database_user"` | ||
DatabasePassword string `toml:"database_password"` | ||
DatabaseName string `toml:"database_name"` | ||
DatabasePort string `toml:"database_port"` | ||
}{ | ||
DatabaseAddress: "test_database_address", | ||
DatabaseUser: "test_database_user", | ||
DatabasePassword: "test_database_password", | ||
DatabaseName: "test_database_name", | ||
DatabasePort: "666", | ||
} | ||
|
||
bytes, err := toml.Marshal(missing_webserver_port) | ||
assert.Nil(t, err) | ||
|
||
filepath, err := writeToml(bytes) | ||
assert.Nil(t, err) | ||
|
||
_, err = ReadConfigToml(filepath) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestMissingDatabasePort(t *testing.T) { | ||
missing_database_address := struct { | ||
DatabaseAddress string `toml:"database_address"` | ||
DatabaseUser string `toml:"database_user"` | ||
DatabasePassword string `toml:"database_password"` | ||
DatabaseName string `toml:"database_name"` | ||
WebserverPort string `toml:"webserver_port"` | ||
}{ | ||
DatabaseAddress: "test_database_address", | ||
DatabaseUser: "test_database_user", | ||
DatabasePassword: "test_database_password", | ||
DatabaseName: "test_database_name", | ||
WebserverPort: "99999", | ||
} | ||
|
||
bytes, err := toml.Marshal(missing_database_address) | ||
assert.Nil(t, err) | ||
|
||
filepath, err := writeToml(bytes) | ||
assert.Nil(t, err) | ||
|
||
_, err = ReadConfigToml(filepath) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestWrongDatabasePortValueType(t *testing.T) { | ||
missing_database_address := struct { | ||
DatabaseAddress string `toml:"database_address"` | ||
DatabaseUser string `toml:"database_user"` | ||
DatabasePassword string `toml:"database_password"` | ||
DatabaseName string `toml:"database_name"` | ||
DatabasePort string `toml:"database_port"` | ||
WebserverPort int `toml:"webserver_port"` | ||
}{ | ||
DatabaseAddress: "test_database_address", | ||
DatabaseUser: "test_database_user", | ||
DatabasePassword: "test_database_password", | ||
DatabaseName: "test_database_name", | ||
DatabasePort: "String Should Not Work", | ||
WebserverPort: 99999, | ||
} | ||
|
||
bytes, err := toml.Marshal(missing_database_address) | ||
assert.Nil(t, err) | ||
|
||
filepath, err := writeToml(bytes) | ||
assert.Nil(t, err) | ||
|
||
_, err = ReadConfigToml(filepath) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestWrongwebserverPortValueType(t *testing.T) { | ||
missing_database_address := struct { | ||
DatabaseAddress string `toml:"database_address"` | ||
DatabaseUser string `toml:"database_user"` | ||
DatabasePassword string `toml:"database_password"` | ||
DatabaseName string `toml:"database_name"` | ||
DatabasePort int `toml:"database_port"` | ||
WebserverPort string `toml:"webserver_port"` | ||
}{ | ||
DatabaseAddress: "test_database_address", | ||
DatabaseUser: "test_database_user", | ||
DatabasePassword: "test_database_password", | ||
DatabaseName: "test_database_name", | ||
DatabasePort: 10, | ||
WebserverPort: "99999", | ||
} | ||
|
||
bytes, err := toml.Marshal(missing_database_address) | ||
assert.Nil(t, err) | ||
|
||
filepath, err := writeToml(bytes) | ||
assert.Nil(t, err) | ||
|
||
_, err = ReadConfigToml(filepath) | ||
assert.NotNil(t, 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
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,18 @@ | ||
# Address to the MariaDB database | ||
database_address = "localhost" | ||
|
||
# User to access datbaase | ||
database_user = "root" | ||
|
||
# Password for the database user | ||
database_password = "root" | ||
|
||
# port | ||
database_port = 3006 | ||
|
||
# name of database where urchin's | ||
# migrations was installed | ||
database_name = "urchin" | ||
|
||
# port to run the webserver on | ||
webserver_port = 8080 |