-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new(cmd,internal/utils,pkg/driver): use correct
engine.kind
config …
…key. Moreover, added a new ReplaceTextInFile utils, and added tests for it and ReplaceLineInFile. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
- Loading branch information
Showing
5 changed files
with
308 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright (C) 2023 The Falco Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package utils | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReplaceTextInFile(t *testing.T) { | ||
tests := []struct { | ||
fileContent string | ||
searchFor string | ||
replacementText string | ||
n int | ||
expectedFileContent string | ||
}{ | ||
{ | ||
fileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
`, | ||
searchFor: "test1: 0", | ||
replacementText: "test1: 1", | ||
n: 1, | ||
expectedFileContent: ` | ||
foo: | ||
bar: | ||
- test1: 1 | ||
- test2: 0 | ||
`, | ||
}, | ||
// text not found | ||
{ | ||
fileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
`, | ||
searchFor: "test3: 0", | ||
replacementText: "test3: 1", | ||
n: 1, | ||
expectedFileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
`, | ||
}, | ||
// N == 0 -> no replacements | ||
{ | ||
fileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
`, | ||
searchFor: "test1: 0", | ||
replacementText: "test1: 1", | ||
n: 0, | ||
expectedFileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
`, | ||
}, | ||
// multiple replacements | ||
{ | ||
fileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
- test3: 0 | ||
`, | ||
searchFor: "test", | ||
replacementText: "testFoo", | ||
n: -1, | ||
expectedFileContent: ` | ||
foo: | ||
bar: | ||
- testFoo1: 0 | ||
- testFoo2: 0 | ||
- testFoo3: 0 | ||
`, | ||
}, | ||
} | ||
|
||
file, err := os.Create("test.txt") | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
_ = file.Close() | ||
_ = os.Remove("test.txt") | ||
}) | ||
|
||
for _, test := range tests { | ||
// Truncate and seek at beginning | ||
err = file.Truncate(0) | ||
require.NoError(t, err) | ||
_, err = file.Seek(0, 0) | ||
require.NoError(t, err) | ||
|
||
_, err = file.WriteString(test.fileContent) | ||
require.NoError(t, err) | ||
|
||
err = ReplaceTextInFile(file.Name(), test.searchFor, test.replacementText, test.n) | ||
require.NoError(t, err) | ||
|
||
content, err := os.ReadFile(file.Name()) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, test.expectedFileContent, string(content)) | ||
} | ||
} | ||
|
||
func TestReplaceLineInFile(t *testing.T) { | ||
tests := []struct { | ||
fileContent string | ||
searchFor string | ||
replacementLine string | ||
n int | ||
expectedFileContent string | ||
}{ | ||
{ | ||
fileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
`, | ||
searchFor: "test1", | ||
replacementLine: "test1: 1", | ||
n: 1, | ||
expectedFileContent: ` | ||
foo: | ||
bar: | ||
test1: 1 | ||
- test2: 0 | ||
`, | ||
}, | ||
// text not found | ||
{ | ||
fileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
`, | ||
searchFor: "test3", | ||
replacementLine: "test3: 1", | ||
n: 1, | ||
expectedFileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
`, | ||
}, | ||
// N == 0 -> no replacements | ||
{ | ||
fileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
`, | ||
searchFor: "test1", | ||
replacementLine: "test1: 1", | ||
n: 0, | ||
expectedFileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
`, | ||
}, | ||
// multiple replacements | ||
{ | ||
fileContent: ` | ||
foo: | ||
bar: | ||
- test1: 0 | ||
- test2: 0 | ||
- test3: 0 | ||
`, | ||
searchFor: "test", | ||
replacementLine: "testFoo", | ||
n: -1, | ||
expectedFileContent: ` | ||
foo: | ||
bar: | ||
testFoo | ||
testFoo | ||
testFoo | ||
`, | ||
}, | ||
} | ||
|
||
file, err := os.Create("test.txt") | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
_ = file.Close() | ||
_ = os.Remove("test.txt") | ||
}) | ||
|
||
for _, test := range tests { | ||
// Truncate and seek at beginning | ||
err = file.Truncate(0) | ||
require.NoError(t, err) | ||
_, err = file.Seek(0, 0) | ||
require.NoError(t, err) | ||
|
||
_, err = file.WriteString(test.fileContent) | ||
require.NoError(t, err) | ||
|
||
err = ReplaceLineInFile(file.Name(), test.searchFor, test.replacementLine, test.n) | ||
require.NoError(t, err) | ||
|
||
content, err := os.ReadFile(file.Name()) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, test.expectedFileContent, string(content)) | ||
} | ||
} |
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.