Skip to content

Commit

Permalink
Replace io/ioutil with preferred packages (#430)
Browse files Browse the repository at this point in the history
* replace: `io/ioutil` with preferred packages.

Since Go 1.16, the `io/ioutil` package has been marked as deprecated.
The functions provided by `io/ioutil` are now provided `io` or `os` packages.

To get more details, please see https://pkg.go.dev/io/ioutil#pkg-overview

* improve: Makefile with Phony targets.

* update: commands in Makefile to be the same as commands on workflows.
  • Loading branch information
FomalhautWeisszwerg committed May 4, 2024
1 parent 0a6a6fc commit 637db4b
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 71 deletions.
32 changes: 25 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
all: deps build test

deps: FORCE
CGO_CFLAGS_ALLOW=-Xpreprocessor go get ./...
.PHONY: deps
deps:
CGO_CFLAGS_ALLOW=-Xpreprocessor go get -v -t -d ./...

build: FORCE
CGO_CFLAGS_ALLOW=-Xpreprocessor go build ./vips
.PHONY: build
build:
CGO_CFLAGS_ALLOW=-Xpreprocessor go build -v ./vips

test: FORCE
CGO_CFLAGS_ALLOW=-Xpreprocessor go test -v ./...
.PHONY: test
test:
CGO_CFLAGS_ALLOW=-Xpreprocessor go test -v -coverprofile=profile.cov ./...

FORCE:
.PHONY: clean
clean:
go clean

.PHONY: clean-cache
clean-cache:
# Purge build cache and test cache.
# When something went wrong on building or testing, try this.
-go clean -testcache
-go clean -cache

.PHONY: distclean
distclean:
-go clean -testcache
-go clean -cache
-git clean -f -x
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The intent for this is to enable developers to build extremely fast image proces

- [libvips](https://github.com/libvips/libvips) 8.10+
- C compatible compiler such as gcc 4.6+ or clang 3.0+
- Go 1.14+
- Go 1.16+

## Dependencies

Expand Down Expand Up @@ -53,7 +53,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"

"github.com/davidbyttow/govips/v2/vips"
Expand All @@ -79,7 +78,7 @@ func main() {

ep := vips.NewDefaultJPEGExportParams()
image1bytes, _, err := image1.Export(ep)
err = ioutil.WriteFile("output.jpg", image1bytes, 0644)
err = os.WriteFile("output.jpg", image1bytes, 0644)
checkError(err)

}
Expand Down
3 changes: 1 addition & 2 deletions examples/jpeg/example_mozjpeg_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package vips_test

import (
"fmt"
"io/ioutil"
"os"

"github.com/davidbyttow/govips/v2/vips"
Expand Down Expand Up @@ -44,5 +43,5 @@ func ExampleMozJPEGEncode() {

imageBytes, _, err := inputImage.ExportJpeg(ep)
checkError(err)
checkError(ioutil.WriteFile("examples/jpeg/mozjpeg-output-govips.jpeg", imageBytes, 0644))
checkError(os.WriteFile("examples/jpeg/mozjpeg-output-govips.jpeg", imageBytes, 0644))
}
3 changes: 1 addition & 2 deletions examples/logging/example_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package package_test

import (
"fmt"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -64,6 +63,6 @@ func main() {

image1bytes, _, err := image1.ExportJpeg(nil)
checkError(err)
err = ioutil.WriteFile("output.jpg", image1bytes, 0644)
err = os.WriteFile("output.jpg", image1bytes, 0644)
checkError(err)
}
9 changes: 5 additions & 4 deletions examples/thumbnail/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package thumbnail

import (
"fmt"
"github.com/davidbyttow/govips/v2/vips"
"io/ioutil"
"os"
"testing"

"github.com/davidbyttow/govips/v2/vips"
)

var file = "../../resources/jpg-24bit-icc-iec.jpg"
Expand Down Expand Up @@ -43,7 +44,7 @@ func BenchmarkNewImageFromFile(b *testing.B) {

func BenchmarkNewImageFromBuffer(b *testing.B) {
resizeToTest := func(size int) {
buf, err := ioutil.ReadFile(file)
buf, err := os.ReadFile(file)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -94,7 +95,7 @@ func BenchmarkNewThumbnailFromFile(b *testing.B) {

func BenchmarkNewThumbnailFromBuffer(b *testing.B) {
resizeToTest := func(size int) {
buf, err := ioutil.ReadFile(file)
buf, err := os.ReadFile(file)
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions examples/tiff/example_tiff_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package package_test

import (
"fmt"
"io/ioutil"
"os"

"github.com/davidbyttow/govips/v2/vips"
Expand Down Expand Up @@ -33,6 +32,6 @@ func main() {
exportParams := vips.NewTiffExportParams()
exportParams.Quality = 100
imageBytes, _, err := inputImage.ExportTiff(exportParams)
err = ioutil.WriteFile("examples/tiff/output-govips.tiff", imageBytes, 0644)
err = os.WriteFile("examples/tiff/output-govips.tiff", imageBytes, 0644)
checkError(err)
}
28 changes: 14 additions & 14 deletions vips/foreign_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package vips

import (
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -10,7 +10,7 @@ import (
func Test_DetermineImageType__JPEG(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "jpg-24bit-icc-iec.jpg")
buf, err := os.ReadFile(resources + "jpg-24bit-icc-iec.jpg")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -21,7 +21,7 @@ func Test_DetermineImageType__JPEG(t *testing.T) {
func Test_DetermineImageType__HEIF_HEIC(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "heic-24bit-exif.heic")
buf, err := os.ReadFile(resources + "heic-24bit-exif.heic")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -32,7 +32,7 @@ func Test_DetermineImageType__HEIF_HEIC(t *testing.T) {
func Test_DetermineImageType__HEIF_MIF1(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "heic-24bit.heic")
buf, err := os.ReadFile(resources + "heic-24bit.heic")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -43,7 +43,7 @@ func Test_DetermineImageType__HEIF_MIF1(t *testing.T) {
func Test_DetermineImageType__PNG(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "png-24bit+alpha.png")
buf, err := os.ReadFile(resources + "png-24bit+alpha.png")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -54,7 +54,7 @@ func Test_DetermineImageType__PNG(t *testing.T) {
func Test_DetermineImageType__TIFF(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "tif.tif")
buf, err := os.ReadFile(resources + "tif.tif")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -65,7 +65,7 @@ func Test_DetermineImageType__TIFF(t *testing.T) {
func Test_DetermineImageType__WEBP(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "webp+alpha.webp")
buf, err := os.ReadFile(resources + "webp+alpha.webp")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -76,7 +76,7 @@ func Test_DetermineImageType__WEBP(t *testing.T) {
func Test_DetermineImageType__SVG(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "svg.svg")
buf, err := os.ReadFile(resources + "svg.svg")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -87,7 +87,7 @@ func Test_DetermineImageType__SVG(t *testing.T) {
func Test_DetermineImageType__SVG_1(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "svg_1.svg")
buf, err := os.ReadFile(resources + "svg_1.svg")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -98,7 +98,7 @@ func Test_DetermineImageType__SVG_1(t *testing.T) {
func Test_DetermineImageType__PDF(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "pdf.pdf")
buf, err := os.ReadFile(resources + "pdf.pdf")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -109,7 +109,7 @@ func Test_DetermineImageType__PDF(t *testing.T) {
func Test_DetermineImageType__BMP(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "bmp.bmp")
buf, err := os.ReadFile(resources + "bmp.bmp")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -120,7 +120,7 @@ func Test_DetermineImageType__BMP(t *testing.T) {
func Test_DetermineImageType__AVIF(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "avif-8bit.avif")
buf, err := os.ReadFile(resources + "avif-8bit.avif")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -131,7 +131,7 @@ func Test_DetermineImageType__AVIF(t *testing.T) {
func Test_DetermineImageType__JP2K(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "jp2k-orientation-6.jp2")
buf, err := os.ReadFile(resources + "jp2k-orientation-6.jp2")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand All @@ -142,7 +142,7 @@ func Test_DetermineImageType__JP2K(t *testing.T) {
func Test_DetermineImageType__JXL(t *testing.T) {
Startup(&Config{})

buf, err := ioutil.ReadFile(resources + "jxl-8bit-grey-icc-dot-gain.jxl")
buf, err := os.ReadFile(resources + "jxl-8bit-grey-icc-dot-gain.jxl")
assert.NoError(t, err)
assert.NotNil(t, buf)

Expand Down
6 changes: 3 additions & 3 deletions vips/icc_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package vips

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)

Expand Down Expand Up @@ -659,14 +659,14 @@ func initializeICCProfiles() {
}

func storeIccProfile(path string, data []byte) {
err := ioutil.WriteFile(path, data, 0600)
err := os.WriteFile(path, data, 0600)
if err != nil {
panic(fmt.Sprintf("Couldn't store temporary file for ICC profile in '%v': %v", path, err.Error()))
}
}

func temporaryDirectoryOrPanic() string {
temporaryDirectory, err := ioutil.TempDir("", "govips-")
temporaryDirectory, err := os.MkdirTemp("", "govips-")
if err != nil {
panic(fmt.Sprintf("Couldn't create temporary directory: %v", err.Error()))
}
Expand Down
7 changes: 4 additions & 3 deletions vips/icc_profiles_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package vips

import (
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
)

Expand All @@ -18,7 +19,7 @@ func Test_ICCProfileInitialisation(t *testing.T) {
}

func assertIccProfile(t *testing.T, expectedProfile []byte, path string) {
loadedProfile, err := ioutil.ReadFile(path)
loadedProfile, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t, expectedProfile, loadedProfile)
}
8 changes: 4 additions & 4 deletions vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"image"
"io"
"io/ioutil"
"os"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -415,7 +415,7 @@ func NewJxlExportParams() *JxlExportParams {

// NewImageFromReader loads an ImageRef from the given reader
func NewImageFromReader(r io.Reader) (*ImageRef, error) {
buf, err := ioutil.ReadAll(r)
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand All @@ -430,7 +430,7 @@ func NewImageFromFile(file string) (*ImageRef, error) {

// LoadImageFromFile loads an image from file and creates a new ImageRef
func LoadImageFromFile(file string, params *ImportParams) (*ImageRef, error) {
buf, err := ioutil.ReadFile(file)
buf, err := os.ReadFile(file)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -817,7 +817,7 @@ func (r *ImageRef) SetPageDelay(delay []int) error {
}

// Export creates a byte array of the image for use.
// The function returns a byte array that can be written to a file e.g. via ioutil.WriteFile().
// The function returns a byte array that can be written to a file e.g. via os.WriteFile().
// N.B. govips does not currently have built-in support for directly exporting to a file.
// The function also returns a copy of the image metadata as well as an error.
// Deprecated: Use ExportNative or format-specific Export methods
Expand Down
10 changes: 5 additions & 5 deletions vips/image_golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"image"
jpeg2 "image/jpeg"
"image/png"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
Expand Down Expand Up @@ -1078,7 +1078,7 @@ func goldenCreateTest(

assertGoldenMatch(t, path, buf, metadata.Format)

buf2, err := ioutil.ReadFile(path)
buf2, err := os.ReadFile(path)
require.NoError(t, err)

img2, err := createFromBuffer(buf2)
Expand Down Expand Up @@ -1137,12 +1137,12 @@ func assertGoldenMatch(t *testing.T, file string, buf []byte, format ImageType)
ext := format.FileExt()
goldenFile := prefix + "-" + getEnvironment() + ".golden" + ext

golden, _ := ioutil.ReadFile(goldenFile)
golden, _ := os.ReadFile(goldenFile)
if golden != nil {
sameAsGolden := assert.True(t, bytes.Equal(buf, golden), "Actual image (size=%d) didn't match expected golden file=%s (size=%d)", len(buf), goldenFile, len(golden))
if !sameAsGolden {
failed := prefix + "-" + getEnvironment() + ".failed" + ext
err := ioutil.WriteFile(failed, buf, 0666)
err := os.WriteFile(failed, buf, 0666)
if err != nil {
panic(err)
}
Expand All @@ -1151,7 +1151,7 @@ func assertGoldenMatch(t *testing.T, file string, buf []byte, format ImageType)
}

t.Log("writing golden file: " + goldenFile)
err := ioutil.WriteFile(goldenFile, buf, 0644)
err := os.WriteFile(goldenFile, buf, 0644)
assert.NoError(t, err)
}

Expand Down
Loading

0 comments on commit 637db4b

Please sign in to comment.