1
1
package config
2
2
3
3
import (
4
+ "bytes"
4
5
"encoding/json"
5
6
"testing"
6
7
"time"
@@ -29,13 +30,13 @@ func TestDuration(t *testing.T) {
29
30
})
30
31
31
32
t .Run ("default value" , func (t * testing.T ) {
32
- for _ , jsonStr := range []string {"null" , "\" \" " , "\" null \" " } {
33
+ for _ , jsonStr := range []string {"null" , "\" \" " , "\" default \" " } {
33
34
var d Duration
34
35
if ! d .IsDefault () {
35
36
t .Fatal ("expected value to be the default initially" )
36
37
}
37
38
if err := json .Unmarshal ([]byte (jsonStr ), & d ); err != nil {
38
- t .Fatal ( err )
39
+ t .Fatalf ( "%s failed to unmarshall with %s" , jsonStr , err )
39
40
}
40
41
if dur := d .WithDefault (time .Hour ); dur != time .Hour {
41
42
t .Fatalf ("expected default value to be used, got %s" , dur )
@@ -59,41 +60,45 @@ func TestDuration(t *testing.T) {
59
60
}
60
61
})
61
62
62
- for jsonStr , goValue := range map [string ]Duration {
63
- "\" \" " : {},
64
- "null" : {},
65
- "\" null\" " : {},
66
- "\" 1s\" " : {value : makeDurationPointer (time .Second )},
67
- "\" 42h1m3s\" " : {value : makeDurationPointer (42 * time .Hour + 1 * time .Minute + 3 * time .Second )},
68
- } {
69
- var d Duration
70
- err := json .Unmarshal ([]byte (jsonStr ), & d )
71
- if err != nil {
72
- t .Fatal (err )
73
- }
63
+ t .Run ("roundtrip including the default values" , func (t * testing.T ) {
64
+ for jsonStr , goValue := range map [string ]Duration {
65
+ // there are various footguns user can hit, normalize them to the canonical default
66
+ "null" : {}, // JSON null → default value
67
+ "\" null\" " : {}, // JSON string "null" sent/set by "ipfs config" cli → default value
68
+ "\" default\" " : {}, // explicit "default" as string
69
+ "\" \" " : {}, // user removed custom value, empty string should also parse as default
70
+ "\" 1s\" " : {value : makeDurationPointer (time .Second )},
71
+ "\" 42h1m3s\" " : {value : makeDurationPointer (42 * time .Hour + 1 * time .Minute + 3 * time .Second )},
72
+ } {
73
+ var d Duration
74
+ err := json .Unmarshal ([]byte (jsonStr ), & d )
75
+ if err != nil {
76
+ t .Fatal (err )
77
+ }
74
78
75
- if goValue .value == nil && d .value == nil {
76
- } else if goValue .value == nil && d .value != nil {
77
- t .Errorf ("expected nil for %s, got %s" , jsonStr , d )
78
- } else if * d .value != * goValue .value {
79
- t .Fatalf ("expected %s for %s, got %s" , goValue , jsonStr , d )
80
- }
79
+ if goValue .value == nil && d .value == nil {
80
+ } else if goValue .value == nil && d .value != nil {
81
+ t .Errorf ("expected nil for %s, got %s" , jsonStr , d )
82
+ } else if * d .value != * goValue .value {
83
+ t .Fatalf ("expected %s for %s, got %s" , goValue , jsonStr , d )
84
+ }
81
85
82
- // Test Reverse
83
- out , err := json .Marshal (goValue )
84
- if err != nil {
85
- t .Fatal (err )
86
- }
87
- if goValue .value == nil {
88
- if string (out ) != "\" null\" " {
89
- t .Fatalf ("expected null string for %s, got %s" , jsonStr , string (out ))
86
+ // Test Reverse
87
+ out , err := json .Marshal (goValue )
88
+ if err != nil {
89
+ t .Fatal (err )
90
+ }
91
+ if goValue .value == nil {
92
+ if ! bytes .Equal (out , []byte ("\" default\" " )) {
93
+ t .Fatalf ("expected default string for %s, got %s" , jsonStr , string (out ))
94
+ }
95
+ continue
96
+ }
97
+ if string (out ) != jsonStr {
98
+ t .Fatalf ("expected %s, got %s" , jsonStr , string (out ))
90
99
}
91
- continue
92
- }
93
- if string (out ) != jsonStr {
94
- t .Fatalf ("expected %s, got %s" , jsonStr , string (out ))
95
100
}
96
- }
101
+ })
97
102
}
98
103
99
104
func TestOneStrings (t * testing.T ) {
0 commit comments