forked from henry40408/concourse-ssh-resource
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(placeholder): Pass stderr to ReplacePlaceholders
Pass stderr to ReplacePlaceholders and add test cases for the function.
- Loading branch information
1 parent
365f3e5
commit b7d718b
Showing
3 changed files
with
119 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package placeholder | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/henry40408/concourse-ssh-resource/internal/models" | ||
) | ||
|
||
const ( | ||
baseDir = "/tmp" | ||
) | ||
|
||
func TestReplacePlaceholdersWithValue(t *testing.T) { | ||
stderr := bytes.NewBuffer([]byte{}) | ||
|
||
fs := afero.NewMemMapFs() | ||
params := &models.Params{ | ||
Script: `echo "<placeholder>"`, | ||
Placeholders: []models.Placeholder{ | ||
models.Placeholder{Name: "<placeholder>", Value: "foobar"}, | ||
}, | ||
} | ||
|
||
script, err := ReplacePlaceholders(stderr, fs, baseDir, params) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, `echo "foobar"`, script) | ||
} | ||
|
||
func TestReplacePlaceholdersWithFile(t *testing.T) { | ||
stderr := bytes.NewBuffer([]byte{}) | ||
|
||
fs := afero.NewMemMapFs() | ||
fs.MkdirAll(baseDir, 0755) | ||
afero.WriteFile(fs, "/tmp/somefile", []byte("foobar"), 0644) | ||
|
||
params := &models.Params{ | ||
Script: `echo "<placeholder>"`, | ||
Placeholders: []models.Placeholder{ | ||
models.Placeholder{Name: "<placeholder>", File: "somefile"}, | ||
}, | ||
} | ||
|
||
script, err := ReplacePlaceholders(stderr, fs, baseDir, params) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Equal(t, `echo "foobar"`, script) | ||
} | ||
|
||
func TestReplacePlaceholdersWithEmptyFile(t *testing.T) { | ||
stderr := bytes.NewBuffer([]byte{}) | ||
|
||
fs := afero.NewMemMapFs() | ||
fs.MkdirAll(baseDir, 0755) | ||
afero.WriteFile(fs, "/tmp/somefile", []byte(""), 0644) | ||
|
||
params := &models.Params{ | ||
Script: `echo "<placeholder>"`, | ||
Placeholders: []models.Placeholder{ | ||
models.Placeholder{Name: "<placeholder>", File: "somefile"}, | ||
}, | ||
} | ||
|
||
_, err := ReplacePlaceholders(stderr, fs, baseDir, params) | ||
if assert.Error(t, err) { | ||
assert.Contains(t, err.Error(), "File for placeholder '<placeholder>' seems to be empty") | ||
} | ||
} | ||
|
||
func TestReplacePlaceholdersWithNonusedPlaceholder(t *testing.T) { | ||
stderr := bytes.NewBuffer([]byte{}) | ||
fs := afero.NewMemMapFs() | ||
params := &models.Params{ | ||
Script: `echo "<placeholder>"`, | ||
Placeholders: []models.Placeholder{ | ||
models.Placeholder{Name: "<place>", Value: "foobar"}, | ||
}, | ||
} | ||
|
||
_, err := ReplacePlaceholders(stderr, fs, baseDir, params) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Contains(t, stderr.String(), "Placeholder '<place>' is not found in script") | ||
} | ||
|
||
func TestReplacePlaceholdersWithPlaceholderWhichHasNoValueNorFile(t *testing.T) { | ||
fs := afero.NewMemMapFs() | ||
|
||
params := &models.Params{ | ||
Script: `echo "<placeholder>"`, | ||
Placeholders: []models.Placeholder{ | ||
models.Placeholder{Name: "<placeholder>"}, | ||
}, | ||
} | ||
|
||
stderr := bytes.NewBuffer([]byte{}) | ||
|
||
_, err := ReplacePlaceholders(stderr, fs, baseDir, params) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
assert.Contains(t, stderr.String(), "Neither File nor Value are set for placeholder '<placeholder>'") | ||
} |
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