| OLD | NEW |
| 1 package builder | 1 package builder |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "fmt" | 4 "fmt" |
| 5 "io/ioutil" | 5 "io/ioutil" |
| 6 "os" | 6 "os" |
| 7 "path/filepath" | 7 "path/filepath" |
| 8 "strings" | 8 "strings" |
| 9 "testing" | 9 "testing" |
| 10 "time" | 10 "time" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 } else { | 80 } else { |
| 81 return nil, fmt.Errorf("Not found") | 81 return nil, fmt.Errorf("Not found") |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 func TestDecimate(t *testing.T) { | 85 func TestDecimate(t *testing.T) { |
| 86 now := time.Now() | 86 now := time.Now() |
| 87 mock := &mockVcs{ | 87 mock := &mockVcs{ |
| 88 commits: map[string]*vcsinfo.LongCommit{ | 88 commits: map[string]*vcsinfo.LongCommit{ |
| 89 "aaa": &vcsinfo.LongCommit{ | 89 "aaa": &vcsinfo.LongCommit{ |
| 90 » » » » Timestamp: now.Add(time.Second), | 90 » » » » Timestamp: now.Add(-62 * 24 * time.Hour), |
| 91 }, | 91 }, |
| 92 "bbb": &vcsinfo.LongCommit{ | 92 "bbb": &vcsinfo.LongCommit{ |
| 93 » » » » Timestamp: now.Add(-2 * time.Second), | 93 » » » » Timestamp: now.Add(-31 * 24 * time.Hour), |
| 94 }, | 94 }, |
| 95 "ccc": &vcsinfo.LongCommit{ | 95 "ccc": &vcsinfo.LongCommit{ |
| 96 » » » » Timestamp: now.Add(-3 * time.Second), | 96 » » » » Timestamp: now.Add(-5 * time.Second), |
| 97 }, | 97 }, |
| 98 "ddd": &vcsinfo.LongCommit{ | 98 "ddd": &vcsinfo.LongCommit{ |
| 99 Timestamp: now.Add(-4 * time.Second), | 99 Timestamp: now.Add(-4 * time.Second), |
| 100 }, | 100 }, |
| 101 "eee": &vcsinfo.LongCommit{ | 101 "eee": &vcsinfo.LongCommit{ |
| 102 » » » » Timestamp: now.Add(-5 * time.Second), | 102 » » » » Timestamp: now.Add(-3 * time.Second), |
| 103 }, | 103 }, |
| 104 "fff": &vcsinfo.LongCommit{ | 104 "fff": &vcsinfo.LongCommit{ |
| 105 » » » » Timestamp: now.Add(-31 * 24 * time.Hour), | 105 » » » » Timestamp: now.Add(-2 * time.Second), |
| 106 }, | 106 }, |
| 107 "ggg": &vcsinfo.LongCommit{ | 107 "ggg": &vcsinfo.LongCommit{ |
| 108 » » » » Timestamp: now.Add(-62 * 24 * time.Hour), | 108 » » » » Timestamp: now.Add(time.Second), |
| 109 }, | 109 }, |
| 110 }, | 110 }, |
| 111 } | 111 } |
| 112 | 112 |
| 113 // No change if number if items < limit. | 113 // No change if number if items < limit. |
| 114 » keep, remove, err := decimate([]string{"aaa", "bbb", "ccc"}, mock, 4) | 114 » keep, remove, err := decimate([]string{"eee", "fff", "ggg"}, mock, 4) |
| 115 assert.NoError(t, err) | 115 assert.NoError(t, err) |
| 116 » assert.Equal(t, keep, []string{"aaa", "bbb", "ccc"}, "") | 116 » assert.Equal(t, keep, []string{"eee", "fff", "ggg"}, "") |
| 117 assert.Equal(t, remove, []string{}) | 117 assert.Equal(t, remove, []string{}) |
| 118 | 118 |
| 119 // Proper decimation if items == limit. | 119 // Proper decimation if items == limit. |
| 120 » keep, remove, err = decimate([]string{"aaa", "bbb", "ccc", "ddd"}, mock,
4) | 120 » keep, remove, err = decimate([]string{"ddd", "eee", "fff", "ggg"}, mock,
4) |
| 121 assert.NoError(t, err) | 121 assert.NoError(t, err) |
| 122 » assert.Equal(t, keep, []string{"aaa", "ccc"}) | 122 » assert.Equal(t, keep, []string{"ddd", "fff", "ggg"}) |
| 123 » assert.Equal(t, remove, []string{"bbb", "ddd"}) | 123 » assert.Equal(t, remove, []string{"eee"}) |
| 124 | 124 |
| 125 // Proper decimation if items > limit. | 125 // Proper decimation if items > limit. |
| 126 » keep, remove, err = decimate([]string{"aaa", "bbb", "ccc", "ddd", "eee"}
, mock, 4) | 126 » keep, remove, err = decimate([]string{"ccc", "ddd", "eee", "fff", "ggg"}
, mock, 4) |
| 127 assert.NoError(t, err) | 127 assert.NoError(t, err) |
| 128 » assert.Equal(t, keep, []string{"aaa", "ccc", "eee"}) | 128 » assert.Equal(t, keep, []string{"ccc", "eee", "ggg"}) |
| 129 » assert.Equal(t, remove, []string{"bbb", "ddd"}) | 129 » assert.Equal(t, remove, []string{"ddd", "fff"}) |
| 130 | 130 |
| 131 // Proper decimation (none) if we end up with less than 'limit' items af
ter removing keepers. | 131 // Proper decimation (none) if we end up with less than 'limit' items af
ter removing keepers. |
| 132 » keep, remove, err = decimate([]string{"aaa", "bbb", "ccc", "fff"}, mock,
4) | 132 » keep, remove, err = decimate([]string{"bbb", "ddd", "eee"}, mock, 4) |
| 133 assert.NoError(t, err) | 133 assert.NoError(t, err) |
| 134 » assert.Equal(t, []string{"aaa", "bbb", "ccc", "fff"}, keep) | 134 » assert.Equal(t, []string{"bbb", "ddd", "eee"}, keep) |
| 135 assert.Equal(t, []string{}, remove) | 135 assert.Equal(t, []string{}, remove) |
| 136 | 136 |
| 137 // Proper decimation (none) if we end up with less than 'limit' items af
ter removing keepers. | 137 // Proper decimation (none) if we end up with less than 'limit' items af
ter removing keepers. |
| 138 // "ccc", "fff", and "ggg" are keepers, leaving just 3 to decimate. | 138 // "ccc", "fff", and "ggg" are keepers, leaving just 3 to decimate. |
| 139 keep, remove, err = decimate([]string{"aaa", "bbb", "ccc", "fff", "ggg"}
, mock, 4) | 139 keep, remove, err = decimate([]string{"aaa", "bbb", "ccc", "fff", "ggg"}
, mock, 4) |
| 140 assert.NoError(t, err) | 140 assert.NoError(t, err) |
| 141 assert.Equal(t, []string{"aaa", "bbb", "ccc", "fff", "ggg"}, keep) | 141 assert.Equal(t, []string{"aaa", "bbb", "ccc", "fff", "ggg"}, keep) |
| 142 assert.Equal(t, []string{}, remove) | 142 assert.Equal(t, []string{}, remove) |
| 143 | 143 |
| 144 // Proper decimation if we end up with enough 'limit' items after removi
ng keepers. | 144 // Proper decimation if we end up with enough 'limit' items after removi
ng keepers. |
| 145 keep, remove, err = decimate([]string{"aaa", "bbb", "ccc", "ddd", "eee",
"fff", "ggg"}, mock, 4) | 145 keep, remove, err = decimate([]string{"aaa", "bbb", "ccc", "ddd", "eee",
"fff", "ggg"}, mock, 4) |
| 146 assert.NoError(t, err) | 146 assert.NoError(t, err) |
| 147 » assert.Equal(t, []string{"aaa", "ccc", "eee", "fff", "ggg"}, keep) | 147 » assert.Equal(t, []string{"aaa", "bbb", "ccc", "eee", "ggg"}, keep) |
| 148 » assert.Equal(t, []string{"bbb", "ddd"}, remove) | 148 » assert.Equal(t, []string{"ddd", "fff"}, remove) |
| 149 } | 149 } |
| 150 | 150 |
| 151 func TestCurrent(t *testing.T) { | 151 func TestCurrent(t *testing.T) { |
| 152 now := time.Now() | 152 now := time.Now() |
| 153 mockRepo := &mockVcs{ | 153 mockRepo := &mockVcs{ |
| 154 commits: map[string]*vcsinfo.LongCommit{ | 154 commits: map[string]*vcsinfo.LongCommit{ |
| 155 "aaa": &vcsinfo.LongCommit{ | 155 "aaa": &vcsinfo.LongCommit{ |
| 156 ShortCommit: &vcsinfo.ShortCommit{ | 156 ShortCommit: &vcsinfo.ShortCommit{ |
| 157 Hash: "aaa", | 157 Hash: "aaa", |
| 158 }, | 158 }, |
| 159 Timestamp: now.Add(time.Second), | 159 Timestamp: now.Add(time.Second), |
| 160 }, | 160 }, |
| 161 }, | 161 }, |
| 162 } | 162 } |
| 163 testData := []string{ | 163 testData := []string{ |
| 164 "aaa", | 164 "aaa", |
| 165 } | 165 } |
| 166 b, cleanup := setupTemp(t, testData, mockRepo) | 166 b, cleanup := setupTemp(t, testData, mockRepo) |
| 167 defer cleanup() | 167 defer cleanup() |
| 168 assert.Equal(t, "aaa", b.Current().Hash) | 168 assert.Equal(t, "aaa", b.Current().Hash) |
| 169 } | 169 } |
| 170 | 170 |
| 171 func TestCurrentNoBuilds(t *testing.T) { | 171 func TestCurrentNoBuilds(t *testing.T) { |
| 172 mockRepo := &mockVcs{} | 172 mockRepo := &mockVcs{} |
| 173 testData := []string{} | 173 testData := []string{} |
| 174 b, cleanup := setupTemp(t, testData, mockRepo) | 174 b, cleanup := setupTemp(t, testData, mockRepo) |
| 175 defer cleanup() | 175 defer cleanup() |
| 176 assert.Equal(t, "unknown", b.Current().Hash) | 176 assert.Equal(t, "unknown", b.Current().Hash) |
| 177 } | 177 } |
| OLD | NEW |