|
| 1 | +Go Lineinfile tests: |
| 2 | + |
| 3 | + $ CURRENT="$(pwd)" |
| 4 | + $ cd "$TESTDIR/../" |
| 5 | + $ go build -o goreplace |
| 6 | + $ cd "$CURRENT" |
| 7 | + $ alias goreplace="$TESTDIR/../goreplace" |
| 8 | + |
| 9 | +Exec test: |
| 10 | + |
| 11 | + $ goreplace -h > /dev/null |
| 12 | + [1] |
| 13 | + |
| 14 | + |
| 15 | +Testing lineinfile mode: |
| 16 | + |
| 17 | + $ cat > test.txt <<EOF |
| 18 | + > this is a testline |
| 19 | + > this is the second line |
| 20 | + > this is the third foobar line |
| 21 | + > this is the last line |
| 22 | + > EOF |
| 23 | + $ goreplace --mode=lineinfile -s foobar -r ___xxx test.txt |
| 24 | + $ cat test.txt |
| 25 | + this is a testline |
| 26 | + this is the second line |
| 27 | + ___xxx |
| 28 | + this is the last line |
| 29 | + |
| 30 | +Testing lineinfile mode with multiple matches: |
| 31 | + |
| 32 | + $ cat > test.txt <<EOF |
| 33 | + > this is a testline |
| 34 | + > this is the second line |
| 35 | + > this is the third foobar line |
| 36 | + > this is the foobar forth foobar line |
| 37 | + > this is the last line |
| 38 | + > EOF |
| 39 | + $ goreplace --mode=lineinfile -s foobar -r ___xxx test.txt |
| 40 | + $ cat test.txt |
| 41 | + this is a testline |
| 42 | + this is the second line |
| 43 | + ___xxx |
| 44 | + ___xxx |
| 45 | + this is the last line |
| 46 | + |
| 47 | +Testing lineinfile mode with multiple matches and --once: |
| 48 | + |
| 49 | + $ cat > test.txt <<EOF |
| 50 | + > this is a testline |
| 51 | + > this is the second line |
| 52 | + > this is the third foobar line |
| 53 | + > this is the foobar forth foobar line |
| 54 | + > this is the last line |
| 55 | + > EOF |
| 56 | + $ goreplace --mode=lineinfile -s foobar -r ___xxx --once test.txt |
| 57 | + $ cat test.txt |
| 58 | + this is a testline |
| 59 | + this is the second line |
| 60 | + ___xxx |
| 61 | + this is the foobar forth foobar line |
| 62 | + this is the last line |
| 63 | + |
| 64 | +Testing lineinfile mode with multiple matches and --once=unique: |
| 65 | + |
| 66 | + $ cat > test.txt <<EOF |
| 67 | + > this is a testline |
| 68 | + > this is the second line |
| 69 | + > this is the third foobar line |
| 70 | + > this is the foobar forth foobar line |
| 71 | + > this is the last line |
| 72 | + > EOF |
| 73 | + $ goreplace --mode=lineinfile -s foobar -r ___xxx --once=unique test.txt |
| 74 | + $ cat test.txt |
| 75 | + this is a testline |
| 76 | + this is the second line |
| 77 | + ___xxx |
| 78 | + this is the last line |
| 79 | + |
| 80 | +Testing lineinfile mode without match: |
| 81 | + |
| 82 | + $ cat > test.txt <<EOF |
| 83 | + > this is a testline |
| 84 | + > this is the second line |
| 85 | + > this is the third foobar line |
| 86 | + > this is the foobar forth foobar line |
| 87 | + > this is the last line |
| 88 | + > EOF |
| 89 | + $ goreplace --mode=lineinfile -s barfoo -r ___xxx --once=unique test.txt |
| 90 | + $ cat test.txt |
| 91 | + this is a testline |
| 92 | + this is the second line |
| 93 | + this is the third foobar line |
| 94 | + this is the foobar forth foobar line |
| 95 | + this is the last line |
| 96 | + ___xxx |
| 97 | + |
| 98 | +Testing lineinfile mode with regex: |
| 99 | + |
| 100 | + $ cat > test.txt <<EOF |
| 101 | + > this is a testline |
| 102 | + > this is the second line |
| 103 | + > this is the third foobar line |
| 104 | + > this is the last line |
| 105 | + > EOF |
| 106 | + $ goreplace --mode=lineinfile --regex --regex-backrefs -s 'f[o]+(b[a]*r)' -r '___$1' test.txt |
| 107 | + $ cat test.txt |
| 108 | + this is a testline |
| 109 | + this is the second line |
| 110 | + ___bar |
| 111 | + this is the last line |
| 112 | + $ goreplace --mode=lineinfile --regex --regex-backrefs -s 'not-existing-line' -r '___$1' test.txt |
| 113 | + $ cat test.txt |
| 114 | + this is a testline |
| 115 | + this is the second line |
| 116 | + ___bar |
| 117 | + this is the last line |
| 118 | + ___ |
| 119 | + |
| 120 | +Testing lineinfile mode with lineinfile-before: |
| 121 | + |
| 122 | + $ cat > test.txt <<EOF |
| 123 | + > this is a testline |
| 124 | + > #global# |
| 125 | + > this is the second line |
| 126 | + > this is the third foobar line |
| 127 | + > this is the last line |
| 128 | + > EOF |
| 129 | + $ goreplace --mode=lineinfile --lineinfile-before="#global#" -s 'notexisting' -r 'example=foobar' test.txt |
| 130 | + $ cat test.txt |
| 131 | + this is a testline |
| 132 | + example=foobar |
| 133 | + #global# |
| 134 | + this is the second line |
| 135 | + this is the third foobar line |
| 136 | + this is the last line |
| 137 | + |
| 138 | +Testing lineinfile mode with lineinfile-after: |
| 139 | + |
| 140 | + $ cat > test.txt <<EOF |
| 141 | + > this is a testline |
| 142 | + > #global# |
| 143 | + > this is the second line |
| 144 | + > this is the third foobar line |
| 145 | + > this is the last line |
| 146 | + > EOF |
| 147 | + $ goreplace --mode=lineinfile --lineinfile-after="#global#" -s 'notexisting' -r 'example=foobar' test.txt |
| 148 | + $ cat test.txt |
| 149 | + this is a testline |
| 150 | + #global# |
| 151 | + example=foobar |
| 152 | + this is the second line |
| 153 | + this is the third foobar line |
| 154 | + this is the last line |
0 commit comments