Skip to content
Permalink
Browse files

feat(placeholder): Pass stderr to ReplacePlaceholders

Pass stderr to ReplacePlaceholders and add test cases for the function.
  • Loading branch information...
henry40408 committed Dec 12, 2017
1 parent 365f3e5 commit b7d718bf664fc3434efc59f4f705dc98e18666a5
Showing with 119 additions and 5 deletions.
  1. +4 −4 internal/placeholder/placeholder.go
  2. +114 −0 internal/placeholder/placeholder_test.go
  3. +1 −1 internal/ssh/ssh.go
@@ -3,7 +3,7 @@ package placeholder
import (
"bufio"
"fmt"
"os"
"io"
"path/filepath"
"strings"

@@ -12,7 +12,7 @@ import (
)

// ReplacePlaceholders replaces placeholders in Script with Placeholders
func ReplacePlaceholders(fs afero.Fs, baseDir string, params *models.Params) (string, error) {
func ReplacePlaceholders(stderr io.Writer, fs afero.Fs, baseDir string, params *models.Params) (string, error) {
var err error

script := params.Script
@@ -37,13 +37,13 @@ func ReplacePlaceholders(fs afero.Fs, baseDir string, params *models.Params) (st
// static Value
value = placeholder.Value
} else {
fmt.Fprintf(os.Stderr, "WARNING: Neither File nor Value are set for placeholder '%s'", placeholder.Name)
fmt.Fprintf(stderr, "WARNING: Neither File nor Value are set for placeholder '%s'", placeholder.Name)
}

if strings.Contains(script, placeholder.Name) {
script = strings.Replace(script, placeholder.Name, value, -1)
} else {
fmt.Fprintf(os.Stderr, "WARNINIG: Placeholder '%s' is not found in script, maybe a typo?", placeholder.Name)
fmt.Fprintf(stderr, "WARNINIG: Placeholder '%s' is not found in script, maybe a typo?", placeholder.Name)
}
}

@@ -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>'")
}
@@ -39,7 +39,7 @@ func PerformSSHCommand(fs afero.Fs, source *models.Source, params *models.Params
interpreter = "/bin/sh"
}

script, err := placeholder.ReplacePlaceholders(fs, baseDir, params)
script, err := placeholder.ReplacePlaceholders(stderr, fs, baseDir, params)
if err != nil {
return hierr.Errorf(err, "unable to replace placeholders")
}

0 comments on commit b7d718b

Please sign in to comment.
You can’t perform that action at this time.