MCPcopy Index your code
hub / github.com/coder/coder / TestReadFile

Function TestReadFile

agent/agentfiles/files_test.go:112–290  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

110}
111
112func TestReadFile(t *testing.T) {
113 t.Parallel()
114
115 tmpdir := os.TempDir()
116 noPermsFilePath := filepath.Join(tmpdir, "no-perms")
117
118 logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
119 fs := newTestFs(afero.NewMemMapFs(), func(call, file string) error {
120 if file == noPermsFilePath {
121 return os.ErrPermission
122 }
123 return nil
124 })
125 api := agentfiles.NewAPI(logger, fs, nil)
126
127 dirPath := filepath.Join(tmpdir, "a-directory")
128 err := fs.MkdirAll(dirPath, 0o755)
129 require.NoError(t, err)
130
131 filePath := filepath.Join(tmpdir, "file")
132 err = afero.WriteFile(fs, filePath, []byte("content"), 0o644)
133 require.NoError(t, err)
134
135 imagePath := filepath.Join(tmpdir, "file.png")
136 err = afero.WriteFile(fs, imagePath, []byte("not really an image"), 0o644)
137 require.NoError(t, err)
138
139 tests := []struct {
140 name string
141 path string
142 limit int64
143 offset int64
144 bytes []byte
145 mimeType string
146 errCode int
147 error string
148 }{
149 {
150 name: "NoPath",
151 path: "",
152 errCode: http.StatusBadRequest,
153 error: "\"path\" is required",
154 },
155 {
156 name: "RelativePathDotSlash",
157 path: "./relative",
158 errCode: http.StatusBadRequest,
159 error: "file path must be absolute",
160 },
161 {
162 name: "RelativePath",
163 path: "also-relative",
164 errCode: http.StatusBadRequest,
165 error: "file path must be absolute",
166 },
167 {
168 name: "NegativeLimit",
169 path: filePath,

Callers

nothing calls this directly

Calls 12

RoutesMethod · 0.95
NewAPIFunction · 0.92
MkdirAllMethod · 0.80
newTestFsFunction · 0.70
TempDirMethod · 0.65
WriteFileMethod · 0.65
RunMethod · 0.65
GetMethod · 0.65
ServeHTTPMethod · 0.45
EqualMethod · 0.45
ReadAllMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected