| 10 | ) |
| 11 | |
| 12 | func TestRun(t *testing.T) { |
| 13 | // Create a temporary directory for testing |
| 14 | tempDir, err := os.MkdirTemp("", "cheat-installer-test-*") |
| 15 | if err != nil { |
| 16 | t.Fatalf("failed to create temp dir: %v", err) |
| 17 | } |
| 18 | defer os.RemoveAll(tempDir) |
| 19 | |
| 20 | // Save original stdin/stdout |
| 21 | oldStdin := os.Stdin |
| 22 | oldStdout := os.Stdout |
| 23 | defer func() { |
| 24 | os.Stdin = oldStdin |
| 25 | os.Stdout = oldStdout |
| 26 | }() |
| 27 | |
| 28 | tests := []struct { |
| 29 | name string |
| 30 | configs string |
| 31 | confpath string |
| 32 | userInput string |
| 33 | wantErr bool |
| 34 | wantInErr string |
| 35 | checkFiles []string |
| 36 | dontWantFiles []string |
| 37 | }{ |
| 38 | { |
| 39 | name: "user declines community cheatsheets", |
| 40 | configs: `--- |
| 41 | editor: EDITOR_PATH |
| 42 | pager: PAGER_PATH |
| 43 | cheatpaths: |
| 44 | - name: community |
| 45 | path: COMMUNITY_PATH |
| 46 | tags: [ community ] |
| 47 | readonly: true |
| 48 | - name: personal |
| 49 | path: PERSONAL_PATH |
| 50 | tags: [ personal ] |
| 51 | readonly: false |
| 52 | `, |
| 53 | confpath: filepath.Join(tempDir, "conf1", "conf.yml"), |
| 54 | userInput: "n\n", |
| 55 | wantErr: false, |
| 56 | checkFiles: []string{"conf1/conf.yml", "conf1/cheatsheets/personal", "conf1/cheatsheets/work"}, |
| 57 | dontWantFiles: []string{"conf1/cheatsheets/community"}, |
| 58 | }, |
| 59 | { |
| 60 | name: "user accepts but clone fails", |
| 61 | configs: `--- |
| 62 | cheatpaths: |
| 63 | - name: community |
| 64 | path: COMMUNITY_PATH |
| 65 | `, |
| 66 | confpath: filepath.Join(tempDir, "conf2", "conf.yml"), |
| 67 | userInput: "y\n", |
| 68 | wantErr: true, |
| 69 | wantInErr: "failed to clone cheatsheets", |