|
1 | 1 | package cache
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 | 4 | "io/ioutil"
|
6 | 5 | "os"
|
7 | 6 | "testing"
|
8 | 7 |
|
9 | 8 | "github.com/stretchr/testify/assert"
|
10 | 9 | )
|
11 | 10 |
|
12 |
| -func TestSetAndGetAndClear(t *testing.T) { |
13 |
| - tempCacheDir, _ := ioutil.TempDir("", "TestCacheDir-*") |
14 |
| - f, _ := ioutil.TempFile(tempCacheDir, "foo.bar.baz-*") |
15 |
| - |
16 |
| - c := Initialize(tempCacheDir) |
17 |
| - |
18 |
| - // set |
19 |
| - expectedCacheContents := "foo bar baz" |
20 |
| - var buf bytes.Buffer |
21 |
| - buf.Write([]byte(expectedCacheContents)) |
22 |
| - |
23 |
| - r, err := c.Set(f.Name(), &buf) |
24 |
| - assert.NoError(t, err) |
25 |
| - |
26 |
| - b, _ := ioutil.ReadAll(r) |
27 |
| - assert.Equal(t, expectedCacheContents, string(b)) |
28 |
| - |
29 |
| - // get |
30 |
| - actualFile := c.Get(f.Name()) |
31 |
| - actualBytes, _ := ioutil.ReadAll(actualFile) |
32 |
| - assert.Equal(t, expectedCacheContents, string(actualBytes)) |
33 |
| - |
34 |
| - // clear |
| 11 | +func TestRealCache_Clear(t *testing.T) { |
| 12 | + d, _ := ioutil.TempDir("", "TestRealCache_Clear") |
| 13 | + c := Initialize(d) |
35 | 14 | assert.NoError(t, c.Clear())
|
36 |
| - |
37 |
| - // confirm that no cachedir remains |
38 |
| - _, err = os.Stat(tempCacheDir) |
| 15 | + _, err := os.Stat(d) |
39 | 16 | assert.True(t, os.IsNotExist(err))
|
| 17 | + |
| 18 | + t.Run("sad path, cache dir doesn't exist", func(t *testing.T) { |
| 19 | + c := Initialize(".") |
| 20 | + assert.Equal(t, "failed to remove cache", c.Clear().Error()) |
| 21 | + }) |
40 | 22 | }
|
0 commit comments