mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-16 04:12:16 +01:00
adding first tests + small refactoring for tests
This commit is contained in:
@@ -26,8 +26,8 @@ type App struct {
|
||||
|
||||
// NewApp instantiates the application by loading the configuration from the
|
||||
// gotenberg.yml file.
|
||||
func NewApp(version string) (*App, error) {
|
||||
c, err := config.NewAppConfig()
|
||||
func NewApp(version string, configurationFilePath string) (*App, error) {
|
||||
c, err := config.NewAppConfig(configurationFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
30
app/app_test.go
Normal file
30
app/app_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewApp(t *testing.T) {
|
||||
// case 1: uses an empty configuration file path.
|
||||
if _, err := NewApp("tests", ""); err == nil {
|
||||
t.Error("App should not have been instantiated!")
|
||||
}
|
||||
|
||||
// case 2: uses an correct configuration file.
|
||||
path, _ := filepath.Abs("../_tests/configurations/gotenberg.yml")
|
||||
if _, err := NewApp("tests", path); err != nil {
|
||||
t.Error("App should have been instantiated!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
path, _ := filepath.Abs("../_tests/configurations/gotenberg.yml")
|
||||
a, _ := NewApp("tests", path)
|
||||
|
||||
quit := make(chan bool, 1)
|
||||
go func() {
|
||||
a.Run()
|
||||
}()
|
||||
quit <- true
|
||||
}
|
||||
@@ -56,8 +56,8 @@ type (
|
||||
|
||||
// NewAppConfig instantiates the application's configuration.
|
||||
// If something bad happens here, the application should not start.
|
||||
func NewAppConfig() (*AppConfig, error) {
|
||||
fileConfig, err := loadFileConfig()
|
||||
func NewAppConfig(configurationFilePath string) (*AppConfig, error) {
|
||||
fileConfig, err := loadFileConfig(configurationFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -131,12 +131,9 @@ type fileConfig struct {
|
||||
} `yaml:"commands"`
|
||||
}
|
||||
|
||||
// configurationFilePath is our default configuration file to parse.
|
||||
const configurationFilePath = "gotenberg.yml"
|
||||
|
||||
// loadFileConfig instantiates a fileConfig instance by loading
|
||||
// the configuration file gotenberg.yml.
|
||||
func loadFileConfig() (*fileConfig, error) {
|
||||
func loadFileConfig(configurationFilePath string) (*fileConfig, error) {
|
||||
c := &fileConfig{}
|
||||
|
||||
data, err := ioutil.ReadFile(configurationFilePath)
|
||||
|
||||
55
app/config/config_test.go
Normal file
55
app/config/config_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewAppConfig(t *testing.T) {
|
||||
// case 1: uses an empty configuration file path.
|
||||
if _, err := NewAppConfig(""); err == nil {
|
||||
t.Error("AppConfig should not have been instantiated!")
|
||||
}
|
||||
|
||||
// case 2: uses a broken configuration file.
|
||||
path, _ := filepath.Abs("../../_tests/configurations/broken-gotenberg.yml")
|
||||
if _, err := NewAppConfig(path); err == nil {
|
||||
t.Error("AppConfig should not have been instantiated!")
|
||||
}
|
||||
|
||||
// case 3: uses a configuration file with a wrong logging level.
|
||||
path, _ = filepath.Abs("../../_tests/configurations/wrong-logging-level-gotenberg.yml")
|
||||
if _, err := NewAppConfig(path); err == nil {
|
||||
t.Error("AppConfig should not have been instantiated!")
|
||||
}
|
||||
|
||||
// case 4: uses a configuration file with a wrong logging format.
|
||||
path, _ = filepath.Abs("../../_tests/configurations/wrong-logging-format-gotenberg.yml")
|
||||
if _, err := NewAppConfig(path); err == nil {
|
||||
t.Error("AppConfig should not have been instantiated!")
|
||||
}
|
||||
|
||||
// case 5: uses a configuration file with a wrong HTML command template.
|
||||
path, _ = filepath.Abs("../../_tests/configurations/wrong-html-command-template-gotenberg.yml")
|
||||
if _, err := NewAppConfig(path); err == nil {
|
||||
t.Error("AppConfig should not have been instantiated!")
|
||||
}
|
||||
|
||||
// case 6: uses a configuration file with a wrong Office command template.
|
||||
path, _ = filepath.Abs("../../_tests/configurations/wrong-office-command-template-gotenberg.yml")
|
||||
if _, err := NewAppConfig(path); err == nil {
|
||||
t.Error("AppConfig should not have been instantiated!")
|
||||
}
|
||||
|
||||
// case 7: uses a configuration file with a wrong merge command template.
|
||||
path, _ = filepath.Abs("../../_tests/configurations/wrong-merge-command-template-gotenberg.yml")
|
||||
if _, err := NewAppConfig(path); err == nil {
|
||||
t.Error("AppConfig should not have been instantiated!")
|
||||
}
|
||||
|
||||
// case 8: uses a correct configuration file.
|
||||
path, _ = filepath.Abs("../../_tests/configurations/gotenberg.yml")
|
||||
if _, err := NewAppConfig(path); err != nil {
|
||||
t.Error("AppConfig should have been instantiated!")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user