TestInit asserts that configs are properly initialized
(t *testing.T)
| 9 | |
| 10 | // TestInit asserts that configs are properly initialized |
| 11 | func TestInit(t *testing.T) { |
| 12 | |
| 13 | // initialize a temporary config file |
| 14 | confFile, err := os.CreateTemp("", "cheat-test") |
| 15 | if err != nil { |
| 16 | t.Errorf("failed to create temp file: %v", err) |
| 17 | } |
| 18 | |
| 19 | // clean up the temp file |
| 20 | defer os.Remove(confFile.Name()) |
| 21 | |
| 22 | // initialize the config file |
| 23 | conf := "mock config data" |
| 24 | if err = Init(confFile.Name(), conf); err != nil { |
| 25 | t.Errorf("failed to init config file: %v", err) |
| 26 | } |
| 27 | |
| 28 | // read back the config file contents |
| 29 | bytes, err := os.ReadFile(confFile.Name()) |
| 30 | if err != nil { |
| 31 | t.Errorf("failed to read config file: %v", err) |
| 32 | } |
| 33 | |
| 34 | // assert that the contents were written correctly |
| 35 | got := string(bytes) |
| 36 | if got != conf { |
| 37 | t.Errorf("failed to write configs: want: %s, got: %s", conf, got) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // TestInitCreateDirectory tests that Init creates the directory if it doesn't exist |
| 42 | func TestInitCreateDirectory(t *testing.T) { |