@@ -12,6 +12,24 @@ import (
12
12
"github.com/chriskuehl/fluffy/testfunc"
13
13
)
14
14
15
+ var configWithEverything = []byte (`
16
+ branding = "foo"
17
+ custom_footer_html = "<p>foo</p>"
18
+ abuse_contact_email = "[email protected] "
19
+ max_upload_bytes = 123
20
+ max_multipart_memory_bytes = 456
21
+ home_url = "http://foo.com"
22
+ object_url_pattern = "http://i.foo.com/o/:path:"
23
+ html_url_pattern = "http://i.foo.com/h/:path:"
24
+ forbidden_file_extensions = ["foo", "bar"]
25
+ host = "192.168.1.100"
26
+ port = 5555
27
+
28
+ [filesystem_storage_backend]
29
+ object_root = "/tmp/objects"
30
+ html_root = "/tmp/html"
31
+ ` )
32
+
15
33
func TestLoadConfigTOMLEmptyFile (t * testing.T ) {
16
34
configPath := t .TempDir () + "/config.toml"
17
35
if err := os .WriteFile (configPath , []byte ("" ), 0644 ); err != nil {
@@ -30,31 +48,13 @@ func TestLoadConfigTOMLEmptyFile(t *testing.T) {
30
48
31
49
func TestLoadConfigTOMLWithEverything (t * testing.T ) {
32
50
configPath := t .TempDir () + "/config.toml"
33
- if err := os .WriteFile (configPath , []byte (`
34
- branding = "foo"
35
- custom_footer_html = "<p>foo</p>"
36
- abuse_contact_email = "[email protected] "
37
- max_upload_bytes = 123
38
- max_multipart_memory_bytes = 456
39
- home_url = "http://foo.com"
40
- object_url_pattern = "http://i.foo.com/o/{path}"
41
- html_url_pattern = "http://i.foo.com/h/{path}"
42
- forbidden_file_extensions = ["foo", "bar"]
43
- host = "192.168.1.100"
44
- port = 5555
45
-
46
- [filesystem_storage_backend]
47
- object_root = "/tmp/objects"
48
- html_root = "/tmp/html"
49
- ` ), 0644 ); err != nil {
51
+ if err := os .WriteFile (configPath , configWithEverything , 0644 ); err != nil {
50
52
t .Fatalf ("failed to write file: %v" , err )
51
53
}
52
-
53
54
conf := testfunc .NewConfig ()
54
55
if err := LoadConfigTOML (conf , configPath ); err != nil {
55
56
t .Fatalf ("failed to load config: %v" , err )
56
57
}
57
-
58
58
errs := conf .Validate ()
59
59
if len (errs ) != 0 {
60
60
t .Fatalf ("config validation failed: %v" , errs )
@@ -67,8 +67,8 @@ html_root = "/tmp/html"
67
67
MaxUploadBytes : 123 ,
68
68
MaxMultipartMemoryBytes : 456 ,
69
69
HomeURL : url.URL {Scheme : "http" , Host : "foo.com" },
70
- ObjectURLPattern : url.URL {Scheme : "http" , Host : "i.foo.com" , Path : "/o/{path}" , RawPath : "/o/{ path} " },
71
- HTMLURLPattern : url.URL {Scheme : "http" , Host : "i.foo.com" , Path : "/h/{path}" , RawPath : "/h/{ path} " },
70
+ ObjectURLPattern : url.URL {Scheme : "http" , Host : "i.foo.com" , Path : "/o/: path: " },
71
+ HTMLURLPattern : url.URL {Scheme : "http" , Host : "i.foo.com" , Path : "/h/: path: " },
72
72
ForbiddenFileExtensions : map [string ]struct {}{"foo" : {}, "bar" : {}},
73
73
Host : "192.168.1.100" ,
74
74
Port : 5555 ,
@@ -82,3 +82,40 @@ html_root = "/tmp/html"
82
82
t .Fatalf ("config mismatch (-want +got):\n %s" , diff )
83
83
}
84
84
}
85
+
86
+ func TestRoundtripDumpLoadConfigTOML (t * testing.T ) {
87
+ configPath := t .TempDir () + "/config.toml"
88
+ if err := os .WriteFile (configPath , configWithEverything , 0644 ); err != nil {
89
+ t .Fatalf ("failed to write file: %v" , err )
90
+ }
91
+ conf := testfunc .NewConfig ()
92
+ if err := LoadConfigTOML (conf , configPath ); err != nil {
93
+ t .Fatalf ("failed to load config: %v" , err )
94
+ }
95
+ errs := conf .Validate ()
96
+ if len (errs ) != 0 {
97
+ t .Fatalf ("config validation failed: %v" , errs )
98
+ }
99
+
100
+ newConfigPath := t .TempDir () + "/new_config.toml"
101
+ configText , err := DumpConfigTOML (conf )
102
+ if err != nil {
103
+ t .Fatalf ("failed to dump config: %v" , err )
104
+ }
105
+ if err := os .WriteFile (newConfigPath , []byte (configText ), 0644 ); err != nil {
106
+ t .Fatalf ("failed to write file: %v" , err )
107
+ }
108
+
109
+ newConf := testfunc .NewConfig ()
110
+ if err := LoadConfigTOML (newConf , newConfigPath ); err != nil {
111
+ t .Fatalf ("failed to load config: %v" , err )
112
+ }
113
+ errs = newConf .Validate ()
114
+ if len (errs ) != 0 {
115
+ t .Fatalf ("config validation failed: %v" , errs )
116
+ }
117
+
118
+ if diff := cmp .Diff (conf , newConf ); diff != "" {
119
+ t .Fatalf ("config mismatch (-want +got):\n %s" , diff )
120
+ }
121
+ }
0 commit comments