Skip to content

Commit 637db4b

Browse files
Replace io/ioutil with preferred packages (#430)
* 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.
1 parent 0a6a6fc commit 637db4b

14 files changed

+85
-71
lines changed

Makefile

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
all: deps build test
22

3-
deps: FORCE
4-
CGO_CFLAGS_ALLOW=-Xpreprocessor go get ./...
3+
.PHONY: deps
4+
deps:
5+
CGO_CFLAGS_ALLOW=-Xpreprocessor go get -v -t -d ./...
56

6-
build: FORCE
7-
CGO_CFLAGS_ALLOW=-Xpreprocessor go build ./vips
7+
.PHONY: build
8+
build:
9+
CGO_CFLAGS_ALLOW=-Xpreprocessor go build -v ./vips
810

9-
test: FORCE
10-
CGO_CFLAGS_ALLOW=-Xpreprocessor go test -v ./...
11+
.PHONY: test
12+
test:
13+
CGO_CFLAGS_ALLOW=-Xpreprocessor go test -v -coverprofile=profile.cov ./...
1114

12-
FORCE:
15+
.PHONY: clean
16+
clean:
17+
go clean
18+
19+
.PHONY: clean-cache
20+
clean-cache:
21+
# Purge build cache and test cache.
22+
# When something went wrong on building or testing, try this.
23+
-go clean -testcache
24+
-go clean -cache
25+
26+
.PHONY: distclean
27+
distclean:
28+
-go clean -testcache
29+
-go clean -cache
30+
-git clean -f -x

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The intent for this is to enable developers to build extremely fast image proces
1414

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

1919
## Dependencies
2020

@@ -53,7 +53,6 @@ package main
5353

5454
import (
5555
"fmt"
56-
"io/ioutil"
5756
"os"
5857

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

8079
ep := vips.NewDefaultJPEGExportParams()
8180
image1bytes, _, err := image1.Export(ep)
82-
err = ioutil.WriteFile("output.jpg", image1bytes, 0644)
81+
err = os.WriteFile("output.jpg", image1bytes, 0644)
8382
checkError(err)
8483

8584
}

examples/jpeg/example_mozjpeg_image.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package vips_test
44

55
import (
66
"fmt"
7-
"io/ioutil"
87
"os"
98

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

4544
imageBytes, _, err := inputImage.ExportJpeg(ep)
4645
checkError(err)
47-
checkError(ioutil.WriteFile("examples/jpeg/mozjpeg-output-govips.jpeg", imageBytes, 0644))
46+
checkError(os.WriteFile("examples/jpeg/mozjpeg-output-govips.jpeg", imageBytes, 0644))
4847
}

examples/logging/example_logging_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package package_test
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"log"
76
"os"
87

@@ -64,6 +63,6 @@ func main() {
6463

6564
image1bytes, _, err := image1.ExportJpeg(nil)
6665
checkError(err)
67-
err = ioutil.WriteFile("output.jpg", image1bytes, 0644)
66+
err = os.WriteFile("output.jpg", image1bytes, 0644)
6867
checkError(err)
6968
}

examples/thumbnail/bench_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package thumbnail
22

33
import (
44
"fmt"
5-
"github.com/davidbyttow/govips/v2/vips"
6-
"io/ioutil"
5+
"os"
76
"testing"
7+
8+
"github.com/davidbyttow/govips/v2/vips"
89
)
910

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

4445
func BenchmarkNewImageFromBuffer(b *testing.B) {
4546
resizeToTest := func(size int) {
46-
buf, err := ioutil.ReadFile(file)
47+
buf, err := os.ReadFile(file)
4748
if err != nil {
4849
panic(err)
4950
}
@@ -94,7 +95,7 @@ func BenchmarkNewThumbnailFromFile(b *testing.B) {
9495

9596
func BenchmarkNewThumbnailFromBuffer(b *testing.B) {
9697
resizeToTest := func(size int) {
97-
buf, err := ioutil.ReadFile(file)
98+
buf, err := os.ReadFile(file)
9899
if err != nil {
99100
panic(err)
100101
}

examples/tiff/example_tiff_image.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package package_test
44

55
import (
66
"fmt"
7-
"io/ioutil"
87
"os"
98

109
"github.com/davidbyttow/govips/v2/vips"
@@ -33,6 +32,6 @@ func main() {
3332
exportParams := vips.NewTiffExportParams()
3433
exportParams.Quality = 100
3534
imageBytes, _, err := inputImage.ExportTiff(exportParams)
36-
err = ioutil.WriteFile("examples/tiff/output-govips.tiff", imageBytes, 0644)
35+
err = os.WriteFile("examples/tiff/output-govips.tiff", imageBytes, 0644)
3736
checkError(err)
3837
}

vips/foreign_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package vips
22

33
import (
4-
"io/ioutil"
4+
"os"
55
"testing"
66

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

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

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

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

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

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

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

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

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

57-
buf, err := ioutil.ReadFile(resources + "tif.tif")
57+
buf, err := os.ReadFile(resources + "tif.tif")
5858
assert.NoError(t, err)
5959
assert.NotNil(t, buf)
6060

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

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

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

79-
buf, err := ioutil.ReadFile(resources + "svg.svg")
79+
buf, err := os.ReadFile(resources + "svg.svg")
8080
assert.NoError(t, err)
8181
assert.NotNil(t, buf)
8282

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

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

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

101-
buf, err := ioutil.ReadFile(resources + "pdf.pdf")
101+
buf, err := os.ReadFile(resources + "pdf.pdf")
102102
assert.NoError(t, err)
103103
assert.NotNil(t, buf)
104104

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

112-
buf, err := ioutil.ReadFile(resources + "bmp.bmp")
112+
buf, err := os.ReadFile(resources + "bmp.bmp")
113113
assert.NoError(t, err)
114114
assert.NotNil(t, buf)
115115

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

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

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

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

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

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

vips/icc_profiles.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package vips
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"os"
66
"path/filepath"
77
)
88

@@ -659,14 +659,14 @@ func initializeICCProfiles() {
659659
}
660660

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

668668
func temporaryDirectoryOrPanic() string {
669-
temporaryDirectory, err := ioutil.TempDir("", "govips-")
669+
temporaryDirectory, err := os.MkdirTemp("", "govips-")
670670
if err != nil {
671671
panic(fmt.Sprintf("Couldn't create temporary directory: %v", err.Error()))
672672
}

vips/icc_profiles_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package vips
22

33
import (
4-
"github.com/stretchr/testify/require"
5-
"io/ioutil"
4+
"os"
65
"testing"
76

7+
"github.com/stretchr/testify/require"
8+
89
"github.com/stretchr/testify/assert"
910
)
1011

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

2021
func assertIccProfile(t *testing.T, expectedProfile []byte, path string) {
21-
loadedProfile, err := ioutil.ReadFile(path)
22+
loadedProfile, err := os.ReadFile(path)
2223
require.NoError(t, err)
2324
assert.Equal(t, expectedProfile, loadedProfile)
2425
}

vips/image.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"fmt"
1010
"image"
1111
"io"
12-
"io/ioutil"
12+
"os"
1313
"runtime"
1414
"strconv"
1515
"strings"
@@ -415,7 +415,7 @@ func NewJxlExportParams() *JxlExportParams {
415415

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

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

819819
// Export creates a byte array of the image for use.
820-
// The function returns a byte array that can be written to a file e.g. via ioutil.WriteFile().
820+
// The function returns a byte array that can be written to a file e.g. via os.WriteFile().
821821
// N.B. govips does not currently have built-in support for directly exporting to a file.
822822
// The function also returns a copy of the image metadata as well as an error.
823823
// Deprecated: Use ExportNative or format-specific Export methods

0 commit comments

Comments
 (0)