saveTmpImage saves the provided image data to a temporary file
(t *testing.T, key string, buf []byte)
| 140 | |
| 141 | // saveTmpImage saves the provided image data to a temporary file |
| 142 | func (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 | } |
no test coverage detected