MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / saveTmpImage

Method saveTmpImage

testutil/image_hash_cache_matcher.go:142–168  ·  view source on GitHub ↗

saveTmpImage saves the provided image data to a temporary file

(t *testing.T, key string, buf []byte)

Source from the content-addressed store, hash-verified

140
141// saveTmpImage saves the provided image data to a temporary file
142func (m *ImageHashCacheMatcher) saveTmpImage(t *testing.T, key string, buf []byte) {
143 t.Helper()
144
145 if m.saveTmpImagesPath == "" {
146 return
147 }
148
149 // Detect the image type to get the correct extension
150 ext, err := imagetype.Detect(bytes.NewReader(buf), "", "")
151 require.NoError(t, err)
152
153 // Put all the test images into the same folder regardless of
154 // the test structure (hierarchy).
155 tmpImageFileName := strings.ReplaceAll(filepath.Join(t.Name(), key), "/", "_")
156
157 targetPath := m.makeTargetPath(t, m.saveTmpImagesPath, "", tmpImageFileName, ext.String())
158
159 targetFile, err := os.Create(targetPath)
160 require.NoError(t, err, "failed to create %s target file %s", saveTmpImagesPathEnv, targetPath)
161 defer targetFile.Close()
162
163 // Write the image data to the file
164 _, err = io.Copy(targetFile, bytes.NewReader(buf))
165 require.NoError(t, err, "failed to write to %s target file %s", saveTmpImagesPathEnv, targetPath)
166
167 t.Logf("Saved temporary image to %s", targetPath)
168}

Callers 1

ImageMatchesMethod · 0.95

Calls 4

makeTargetPathMethod · 0.95
DetectFunction · 0.92
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected