Skip to content

Commit 3657e9c

Browse files
authored
Use tmp file in unit-tests to allow for testing portability (#85)
Description of changes: Unit tests fail when not using GNU sed this is because command uses `sed -i` which when not using GNU sed needs to specify a string afterwards which can be defaulted to `sed -i ''` However when attempting this allows for the unit-tests to pass but it created tmp files on my end with suffix ''. Ended up creating .tmp files initially and then removing them, this is consistent with the GNU internal implementation. https://www.gnu.org/software/sed/manual/sed.html#index-_002di > GNU sed does this by creating a temporary file and sending output to this file rather than to the standard output By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 3275772 commit 3657e9c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

pkg/testutil/util.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ import (
1717
"encoding/json"
1818
"errors"
1919
"fmt"
20-
"github.com/aws/aws-sdk-go/aws/awserr"
21-
"github.com/ghodss/yaml"
2220
"io/ioutil"
2321
"os"
2422
"os/exec"
2523
"path"
2624
"strings"
25+
26+
"github.com/aws/aws-sdk-go/aws/awserr"
27+
"github.com/ghodss/yaml"
2728
)
2829

2930
var (
@@ -79,11 +80,16 @@ func IsYamlEqual(expectation *string, actualYamlByteArray *[]byte) bool {
7980
}
8081

8182
// Replace Timestamps that would show up as different.
82-
_, err := exec.Command("sed", "-r", "-i", ReplaceTimestampRegExp, actualYamlFileName).Output()
83+
_, err := exec.Command("sed", "-r", "-i.tmp", ReplaceTimestampRegExp, actualYamlFileName).Output()
8384
if isExecCommandError(err) {
8485
return false
8586
}
8687

88+
// Remove tmp files used as backup https://riptutorial.com/sed/topic/9436/bsd-macos-sed-vs--gnu-sed-vs--the-posix-sed-specification
89+
// -i inplace-editing is not consistent on both GNU and non-GNU sed when not specifiying a backup file.
90+
actualYamlFileNameTmp := actualYamlFileName + ".tmp"
91+
defer os.Remove(actualYamlFileNameTmp)
92+
8793
output, err := exec.Command("diff", "-c", expectedYamlFileName, actualYamlFileName).Output()
8894
if isExecCommandError(err) {
8995
return false

0 commit comments

Comments
 (0)