Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Packer
Commits
be7d7313
Commit
be7d7313
authored
4 years ago
by
Adrien Delorme
Browse files
Options
Download
Email Patches
Plain Diff
add tests for piping fmt
parent
7d30a5d7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
command/fmt_test.go
+52
-20
command/fmt_test.go
with
52 additions
and
20 deletions
+52
-20
command/fmt_test.go
+
52
-
20
View file @
be7d7313
...
...
@@ -2,12 +2,14 @@ package command
import
(
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
packersdk
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/stretchr/testify/assert"
)
...
...
@@ -54,18 +56,20 @@ func TestFmt_unfomattedTemlateDirectory(t *testing.T) {
}
}
func
TestFmt_Recursive
(
t
*
testing
.
T
)
{
unformatted
Data
:
=
`
const
(
unformatted
HCL
=
`
ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2"
ami_filter_owners =[ "137112412989" ]
`
formattedData
:=
`
formattedHCL
=
`
ami_filter_name = "amzn2-ami-hvm-*-x86_64-gp2"
ami_filter_owners = ["137112412989"]
`
)
func
TestFmt_Recursive
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
...
...
@@ -77,34 +81,34 @@ ami_filter_owners = ["137112412989"]
name
:
"nested formats recursively"
,
formatArgs
:
[]
string
{
"-recursive=true"
},
alreadyPresentContent
:
map
[
string
]
string
{
"foo/bar/baz.pkr.hcl"
:
unformatted
Data
,
"foo/bar/baz/woo.pkrvars.hcl"
:
unformatted
Data
,
"potato"
:
unformatted
Data
,
"foo/bar/potato"
:
unformatted
Data
,
"bar.pkr.hcl"
:
unformatted
Data
,
"foo/bar/baz.pkr.hcl"
:
unformatted
HCL
,
"foo/bar/baz/woo.pkrvars.hcl"
:
unformatted
HCL
,
"potato"
:
unformatted
HCL
,
"foo/bar/potato"
:
unformatted
HCL
,
"bar.pkr.hcl"
:
unformatted
HCL
,
},
fileCheck
:
fileCheck
{
expectedContent
:
map
[
string
]
string
{
"foo/bar/baz.pkr.hcl"
:
formatted
Data
,
"foo/bar/baz/woo.pkrvars.hcl"
:
formatted
Data
,
"potato"
:
unformatted
Data
,
"foo/bar/potato"
:
unformatted
Data
,
"bar.pkr.hcl"
:
formatted
Data
,
"foo/bar/baz.pkr.hcl"
:
formatted
HCL
,
"foo/bar/baz/woo.pkrvars.hcl"
:
formatted
HCL
,
"potato"
:
unformatted
HCL
,
"foo/bar/potato"
:
unformatted
HCL
,
"bar.pkr.hcl"
:
formatted
HCL
,
}},
},
{
name
:
"nested no recursive format"
,
formatArgs
:
[]
string
{},
alreadyPresentContent
:
map
[
string
]
string
{
"foo/bar/baz.pkr.hcl"
:
unformatted
Data
,
"foo/bar/baz/woo.pkrvars.hcl"
:
unformatted
Data
,
"bar.pkr.hcl"
:
unformatted
Data
,
"foo/bar/baz.pkr.hcl"
:
unformatted
HCL
,
"foo/bar/baz/woo.pkrvars.hcl"
:
unformatted
HCL
,
"bar.pkr.hcl"
:
unformatted
HCL
,
},
fileCheck
:
fileCheck
{
expectedContent
:
map
[
string
]
string
{
"foo/bar/baz.pkr.hcl"
:
unformatted
Data
,
"foo/bar/baz/woo.pkrvars.hcl"
:
unformatted
Data
,
"bar.pkr.hcl"
:
formatted
Data
,
"foo/bar/baz.pkr.hcl"
:
unformatted
HCL
,
"foo/bar/baz/woo.pkrvars.hcl"
:
unformatted
HCL
,
"bar.pkr.hcl"
:
formatted
HCL
,
}},
},
}
...
...
@@ -138,3 +142,31 @@ ami_filter_owners = ["137112412989"]
})
}
}
func
Test_fmt_pipe
(
t
*
testing
.
T
)
{
tc
:=
[]
struct
{
piped
string
command
[]
string
env
[]
string
expected
string
}{
{
unformattedHCL
,
[]
string
{
"fmt"
,
"-"
},
nil
,
formattedHCL
+
"
\n
"
},
}
for
_
,
tc
:=
range
tc
{
t
.
Run
(
fmt
.
Sprintf
(
"echo %q | packer %s"
,
tc
.
piped
,
tc
.
command
),
func
(
t
*
testing
.
T
)
{
p
:=
helperCommand
(
t
,
tc
.
command
...
)
p
.
Stdin
=
strings
.
NewReader
(
tc
.
piped
)
p
.
Env
=
append
(
p
.
Env
,
tc
.
env
...
)
bs
,
err
:=
p
.
Output
()
if
err
!=
nil
{
t
.
Fatalf
(
"%v: %s"
,
err
,
bs
)
}
if
diff
:=
cmp
.
Diff
(
tc
.
expected
,
string
(
bs
));
diff
!=
""
{
t
.
Fatalf
(
"%s"
,
diff
)
}
})
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help