-
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use ioutil for Go 1.15 and lower (#492)
Since Go 1.16 it has been recommended to no longer use ioutil in new code. We want Gomega to look modern so we removed references to ioutil, which also removes the chance of outdated code being copied. However we got feedback that some users were stuck on Go 1.15 and lower and were broken by this change. We have therefore introduces a "gutil" package that implements similar functions to ioutil and redirect to the right functions appropriate to the version of Go. This will hopefully: - make it look intentional that ioutil is still used (rather than looking like an omission) - reduce the chance of ioutil usage being propagated by copying - limit the possibility of deprecation warnings
- Loading branch information
Showing
16 changed files
with
164 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//go:build go1.16 | ||
// +build go1.16 | ||
|
||
// Package gutil is a replacement for ioutil, which should not be used in new | ||
// code as of Go 1.16. With Go 1.16 and higher, this implementation | ||
// uses the ioutil replacement functions in "io" and "os" with some | ||
// Gomega specifics. This means that we should not get deprecation warnings | ||
// for ioutil when they are added. | ||
package gutil | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
func NopCloser(r io.Reader) io.ReadCloser { | ||
return io.NopCloser(r) | ||
} | ||
|
||
func ReadAll(r io.Reader) ([]byte, error) { | ||
return io.ReadAll(r) | ||
} | ||
|
||
func ReadDir(dirname string) ([]string, error) { | ||
entries, err := os.ReadDir(dirname) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var names []string | ||
for _, entry := range entries { | ||
names = append(names, entry.Name()) | ||
} | ||
|
||
return names, nil | ||
} | ||
|
||
func ReadFile(filename string) ([]byte, error) { | ||
return os.ReadFile(filename) | ||
} | ||
|
||
func MkdirTemp(dir, pattern string) (string, error) { | ||
return os.MkdirTemp(dir, pattern) | ||
} | ||
|
||
func WriteFile(filename string, data []byte) error { | ||
return os.WriteFile(filename, data, 0644) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//go:build !go1.16 | ||
// +build !go1.16 | ||
|
||
// Package gutil is a replacement for ioutil, which should not be used in new | ||
// code as of Go 1.16. With Go 1.15 and lower, this implementation | ||
// uses the ioutil functions, meaning that although Gomega is not officially | ||
// supported on these versions, it is still likely to work. | ||
package gutil | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
) | ||
|
||
func NopCloser(r io.Reader) io.ReadCloser { | ||
return ioutil.NopCloser(r) | ||
} | ||
|
||
func ReadAll(r io.Reader) ([]byte, error) { | ||
return ioutil.ReadAll(r) | ||
} | ||
|
||
func ReadDir(dirname string) ([]string, error) { | ||
files, err := ioutil.ReadDir(dirname) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var names []string | ||
for _, file := range files { | ||
names = append(names, file.Name()) | ||
} | ||
|
||
return names, nil | ||
} | ||
|
||
func ReadFile(filename string) ([]byte, error) { | ||
return ioutil.ReadFile(filename) | ||
} | ||
|
||
func MkdirTemp(dir, pattern string) (string, error) { | ||
return ioutil.TempDir(dir, pattern) | ||
} | ||
|
||
func WriteFile(filename string, data []byte) error { | ||
return ioutil.WriteFile(filename, data, 0644) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.