| 26 | func (r *stdinReader) Close() error { return nil } |
| 27 | |
| 28 | func TestReadMarkdownBodyFile(t *testing.T) { |
| 29 | tests := []struct { |
| 30 | name string |
| 31 | files map[string]string |
| 32 | path string |
| 33 | stdin string |
| 34 | wantContent string |
| 35 | wantDir string |
| 36 | wantErrIs error |
| 37 | }{ |
| 38 | { |
| 39 | name: "a file in the working directory", |
| 40 | files: map[string]string{"body.md": "\n"}, |
| 41 | path: "body.md", |
| 42 | wantContent: "\n", |
| 43 | wantDir: ".", |
| 44 | }, |
| 45 | { |
| 46 | name: "a file in another directory", |
| 47 | files: map[string]string{"docs/body.md": "\n"}, |
| 48 | path: "docs/body.md", |
| 49 | wantContent: "\n", |
| 50 | wantDir: "docs", |
| 51 | }, |
| 52 | { |
| 53 | name: "standard input has no directory", |
| 54 | path: "-", |
| 55 | stdin: "\n", |
| 56 | wantContent: "\n", |
| 57 | wantDir: "", |
| 58 | }, |
| 59 | { |
| 60 | name: "standard input is only read for -", |
| 61 | files: map[string]string{"body.md": "From the file\n"}, |
| 62 | path: "body.md", |
| 63 | stdin: "From standard input\n", |
| 64 | wantContent: "From the file\n", |
| 65 | wantDir: ".", |
| 66 | }, |
| 67 | { |
| 68 | name: "a missing file", |
| 69 | path: "docs/missing.md", |
| 70 | wantErrIs: fs.ErrNotExist, |
| 71 | }, |
| 72 | } |
| 73 | |
| 74 | for _, tt := range tests { |
| 75 | t.Run(tt.name, func(t *testing.T) { |
| 76 | t.Chdir(t.TempDir()) |
| 77 | for path, content := range tt.files { |
| 78 | require.NoError(t, os.MkdirAll(filepath.Dir(path), 0755)) |
| 79 | require.NoError(t, os.WriteFile(path, []byte(content), 0644)) |
| 80 | } |
| 81 | stdin := &stdinReader{Reader: strings.NewReader(tt.stdin)} |
| 82 | |
| 83 | content, dir, err := ReadMarkdownBodyFile(tt.path, stdin) |
| 84 | |
| 85 | assert.Equal(t, tt.path == "-", stdin.read, "standard input is read only for -") |