Skip to content

Commit

Permalink
Updates after PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
morestatic committed Mar 1, 2023
1 parent 9116b71 commit 0009b5b
Show file tree
Hide file tree
Showing 23 changed files with 348 additions and 188 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@

# hugo
docs/public
docs/.hugo_build.lock
docs/.hugo_build.lock

# e2e test output files
tasks/*.txt
tasks/*.*_temp
46 changes: 38 additions & 8 deletions conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strconv"
"strings"

"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -109,6 +110,7 @@ func ConvertToFileMode(val interface{}) (os.FileMode, error) {
}

var (
ErrNotANumber = errors.New("file size is not a number")
ErrFileSizeInvalidUnits = errors.New("file size has invalid units")
)

Expand All @@ -120,21 +122,49 @@ func ConvertToFileSize(val interface{}) (convertedVal uint64, err error) {
valPart := valStr[:valLen-1]
units := valStr[valLen-1:]

multiplier := 1024
switch units {
if !isNumber(valPart) {
return 0, ErrNotANumber
}

// assume bytes to start with
multiplier := 1

// if we have units, work out the multiplier
if !isNumber(units) {
multiplier, err = getMultiplierFromUnits(units)
if err != nil {
return 0, err
}
} else {
// make sure to include the last digit again
valPart = valStr
}

valNum, err := strconv.ParseUint(valPart, 10, 64)
if err != nil {
return 0, err
}
convertedVal = valNum * uint64(multiplier)
return convertedVal, nil
}

func getMultiplierFromUnits(units string) (multiplier int, err error) {
switch strings.ToLower(units) {
case "b":
multiplier = 1
case "k":
multiplier = 1024
case "m":
multiplier = 1024 * 1024
case "g":
multiplier = 1024 * 1024 * 1024
default:
return 0, ErrFileSizeInvalidUnits
}
return multiplier, nil
}

valNum, err := strconv.ParseUint(valPart, 10, 64)
if err != nil {
return 0, err
}
convertedVal = valNum * uint64(multiplier)
return convertedVal, nil
func isNumber(str string) bool {
_, err := strconv.Atoi(str)
return err == nil
}
25 changes: 20 additions & 5 deletions conv/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ func TestShouldConvertFileSizeToUInt64(t *testing.T) {
expectedValue uint64
expectedErr error
}{
{
name: "error when no units",
inputStr: "100",
expectedErr: conv.ErrFileSizeInvalidUnits,
},
{
name: "error when unknown units",
inputStr: "100t",
expectedErr: conv.ErrFileSizeInvalidUnits,
},
{
name: "assume bytes",
inputStr: "100",
expectedValue: 100,
},
{
name: "100b",
inputStr: "100b",
expectedValue: 100,
},
{
name: "100k",
inputStr: "100k",
Expand All @@ -39,6 +44,16 @@ func TestShouldConvertFileSizeToUInt64(t *testing.T) {
inputStr: "300g",
expectedValue: uint64(300 * 1024 * 1024 * 1024),
},
{
name: "error when not a number, but valid units",
inputStr: "bbb",
expectedErr: conv.ErrNotANumber,
},
{
name: "error when not a number",
inputStr: "bbc",
expectedErr: conv.ErrNotANumber,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions docs/content/functions/no02-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ maintain-my-file:
- source: https://github.com/notepad-plus-plus/notepad-plus-plus/releases/download/v7.8.8/npp.7.8.8.Installer.x64.exe
- source_hash: md5=79eef25f9b0b2c642c62b7f737d4f53f
- makedirs: true # default false
- replace: false # default true
- repl: false # default true
- creates: 'C:\Program Files\notepad++\notepad++.exe'
```
Expand Down Expand Up @@ -182,7 +182,7 @@ another-url:
file.managed:
- name: /tmp/sub/some/dir/utf8-js-1.json
- makedirs: true
- replace: false
- repl: false
- user: root
- group: root
- mode: 0755
Expand Down Expand Up @@ -457,7 +457,7 @@ We can read it as following:
Contains the regular expression to search for in the target file. The regular expressions supported use the golang regexp engine.
Group replacements are supported using the standard `()` and `$` notations.

### `replace`
### `repl`

{{< parameter required=1 type=string >}}

Expand Down Expand Up @@ -487,8 +487,8 @@ If set to `true` then either the text specified by the `not_found_content` will

{{< parameter type=string >}}

The text specified by this property will Used when either the `append_if_not_found` or `prepend_if_not_found` parameters
re set to `true`. The text will be appended or prepended to the target file accordingly.
The text specified by this property will be used when either the `append_if_not_found` or `prepend_if_not_found`
parameters are set to `true`. The text will be appended or prepended to the target file accordingly.

### `backup`

Expand Down
2 changes: 1 addition & 1 deletion docs/content/functions/no04-windows-registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This is the `name` of the registry value to ensure is present at the registry pa

{{< parameter required=1 type=string >}}

This is the value that must be is present. If there is no value currently then a new value will be set.
This is the value that must be present. If there is no value currently then a new value will be set.
If there is an existing value then it will be replaced.

### `type`
Expand Down
25 changes: 16 additions & 9 deletions e2etests/debian-pkgs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ Run:
- refresh: true
- name: cowsay
- unless:
- /usr/games/cowsay
- /usr/games/cowsay
install-cowsay-already-installed:
pkg.installed:
- refresh: true
- name: cowsay
- creates:
- /usr/games/cowsay
- /usr/games/cowsay
- require:
- install-cowsay
- install-cowsay
remove-cowsay:
pkg.removed:
- refresh: true
- name: cowsay
- onlyif:
- /usr/games/cowsay
- /usr/games/cowsay

On:
- linux
Expand All @@ -28,14 +28,21 @@ OnlyIf: "[ $(id -u) -eq 0 ] && which apt-get" # Skip test if required package ma
Expect:
PreExec: /usr/games/cowsay ok && apt-get -y remove cowsay || true
Summary:
Succeeded: 1
Changes: 0 # must be 1 @todo: fix tacoscript
Succeeded: 3
Changes: 2
Failed: 0
Aborted: 0
TotalTasksRun: 1
TotalTasksRun: 3
TaskResults:
- ID: install-cowsay
Result: true
ChangesContains:
- "cowsay"
PostExec: /usr/games/cowsay ok
- "added"
- ID: install-cowsay-already-installed
Result: true
CommentContains:
- "not updated"
- ID: remove-cowsay
Result: true
ChangesContains:
- "removed"
74 changes: 61 additions & 13 deletions e2etests/replace-file-unix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,109 @@ Run:
this is a file
created by tacoscript
- creates:
- /tmp/test-file.txt
- /tmp/test-file.txt
replace-file-skip-as-file-exists:
file.replace:
- name: /tmp/test-file.txt
- pattern: this is a file
- repl: this is a modified file
- creates:
- /tmp/test-file.txt
- creates:
- /tmp/test-file.txt
replace-file:
file.replace:
- name: /tmp/test-file.txt
- pattern: this is a file
- repl: this is a replacement file
- onlyif:
- test -f /tmp/test-file.txt
- test -f /tmp/test-file.txt
replace-file-skip-again-as-unless-check:
file.replace:
- name: /tmp/test-file.txt
- pattern: this is a replacement file
- repl: this is a another replacement file
- unless:
- test -f /tmp/test-file.txt
- test -f /tmp/test-file.txt
replace-file-again:
file.replace:
- name: /tmp/test-file.txt
- pattern: this is a replacement file
- repl: this is a another replacement file
- require:
- replace-file
- replace-file
replace-with-backup:
file.replace:
- name: /tmp/test-file.txt
- pattern: tacoscript
- repl: Tacoscript
- backup: backup
- require:
- replace-file
replace-with-append:
file.replace:
- name: /tmp/test-file.txt
- pattern: bunny
- repl: appended this line because a bunny was not found
- append_if_not_found: true
replace-with-prepent:
file.replace:
- name: /tmp/test-file.txt
- pattern: rabbit
- repl: prepended this line because a rabbit was not found
- prepend_if_not_found: true
dont-replace-max-file-size:
file.replace:
- name: /tmp/big.txt
- pattern: ignore
- repl: nothing
- max_file_size: 1k

On:
- darwin
- linux

Expect:
PreExec: test -e /tmp/test-file.txt && rm -f /tmp/test-file.txt ||true
PreExec: |
test -e /tmp/test-file.txt && rm -f /tmp/test-file.txt ||true
openssl rand -base64 1024 >/tmp/big.txt
Summary:
Succeeded: 5
Changes: 3
TotalTasksRun: 5
Succeeded: 9
Changes: 6
TotalTasksRun: 9
TaskResults:
- ID: write-file
ChangesContains:
- "37 bytes written"
- ID: replace-file-skip-as-file-exists
HasChanges: false
CommentContains:
- "File not changed"
- ID: replace-file
ChangesContains:
ChangeContains:
- "1 replacement(s) made"
- ID: replace-file-skip-again-as-unless-check
HasChanges: false
CommentContains:
- "File not changed"
- ID: replace-file-again
ChangeContains:
- "1 replacement(s) made"
- ID: replace-with-backup
ChangesContains:
- "1 replacement(s) made"
- ID: replace-with-append
ChangesContains:
- "1 addition(s) made"
- ID: replace-with-prepent
ChangesContains:
- "1 addition(s) made"
- ID: dont-replace-max-file-size
CommentContains:
- File not changed file size is greater than max_file_size
PostExec: |
grep tacoscript /tmp/test-file.txt
grep "this is a another replacement file" /tmp/test-file.txt
tail -n1 /tmp/test-file.txt|grep "a bunny was not found" /tmp/test-file.txt
head -n1 /tmp/test-file.txt|grep "a rabbit was not found" /tmp/test-file.txt
rm /tmp/test-file.txt
test -e /tmp/test-file.txt.backup
rm /tmp/test-file.txt.backup
rm /tmp/big.txt
10 changes: 5 additions & 5 deletions e2etests/replace-file-win.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ Run:
this is a file
created by tacoscript
- creates:
- C:\Windows\temp\testfile.txt
- C:\Windows\temp\testfile.txt
replace-file-skip-as-file-exists:
file.replace:
- name: C:\Windows\temp\testfile.txt
- pattern: this is a file
- repl: this is a modified file
- unless:
# file exists so the exception will not be thrown
- if (-not (test-path -Path C:\Windows\temp\testfile.txt -PathType leaf)) { throw 'missing' }
- if (-not (test-path -Path C:\Windows\temp\testfile.txt -PathType leaf)) { throw 'missing' }
- shell: powershell.exe
replace-file:
file.replace:
Expand All @@ -23,15 +23,15 @@ Run:
- repl: this is a replacement file
- onlyif:
# file exists so the exception will not be thrown
- if (-not (test-path -Path C:\Windows\temp\testfile.txt -PathType leaf)) { throw 'missing' }
- if (-not (test-path -Path C:\Windows\temp\testfile.txt -PathType leaf)) { throw 'missing' }
- shell: powershell.exe
replace-file-again:
file.replace:
- name: C:\Windows\temp\testfile.txt
- pattern: this is a replacement file
- repl: this is a another replacement file
- require:
- replace-file
- replace-file

On:
- windows
Expand Down Expand Up @@ -63,4 +63,4 @@ Expect:
if(-not (Select-String -Path C:\Windows\temp\testfile.txt -Pattern "created by tacoscript" -Quiet)) {
Write-Error "string not found"
}
rm C:\Windows\temp\testfile.txt -force
rm C:\Windows\temp\testfile.txt -force
Loading

0 comments on commit 0009b5b

Please sign in to comment.