You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.5 KiB

  1. package build_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "gerrit.wikimedia.org/r/blubber/build"
  6. )
  7. func TestRun(t *testing.T) {
  8. i := build.Run{"echo %s %s", []string{"foo bar", "baz"}}
  9. assert.Equal(t, []string{`echo "foo bar" "baz"`}, i.Compile())
  10. }
  11. func TestRunWithInnerAndOuterArguments(t *testing.T) {
  12. i := build.Run{"useradd -d %s -u %s", []string{"/foo", "666", "bar"}}
  13. assert.Equal(t, []string{`useradd -d "/foo" -u "666" "bar"`}, i.Compile())
  14. }
  15. func TestRunAll(t *testing.T) {
  16. i := build.RunAll{[]build.Run{
  17. {"echo %s", []string{"foo"}},
  18. {"cat %s", []string{"/bar"}},
  19. {"baz", []string{}},
  20. }}
  21. assert.Equal(t, []string{`echo "foo" && cat "/bar" && baz`}, i.Compile())
  22. }
  23. func TestCopy(t *testing.T) {
  24. i := build.Copy{[]string{"source1", "source2"}, "dest"}
  25. assert.Equal(t, []string{`"source1"`, `"source2"`, `"dest/"`}, i.Compile())
  26. }
  27. func TestCopyAs(t *testing.T) {
  28. t.Run("wrapping Copy", func(t *testing.T) {
  29. i := build.CopyAs{
  30. 123,
  31. 124,
  32. build.Copy{[]string{"source1", "source2"}, "dest"},
  33. }
  34. assert.Equal(t, []string{"123:124", `"source1"`, `"source2"`, `"dest/"`}, i.Compile())
  35. })
  36. t.Run("wrapping CopyFrom", func(t *testing.T) {
  37. i := build.CopyAs{
  38. 123,
  39. 124,
  40. build.CopyFrom{"foo", build.Copy{[]string{"source1", "source2"}, "dest"}},
  41. }
  42. assert.Equal(t, []string{"123:124", "foo", `"source1"`, `"source2"`, `"dest/"`}, i.Compile())
  43. })
  44. }
  45. func TestCopyFrom(t *testing.T) {
  46. i := build.CopyFrom{"foo", build.Copy{[]string{"source1", "source2"}, "dest"}}
  47. assert.Equal(t, []string{"foo", `"source1"`, `"source2"`, `"dest/"`}, i.Compile())
  48. }
  49. func TestEntryPoint(t *testing.T) {
  50. i := build.EntryPoint{[]string{"/bin/foo", "bar", "baz"}}
  51. assert.Equal(t, []string{`"/bin/foo"`, `"bar"`, `"baz"`}, i.Compile())
  52. }
  53. func TestEnv(t *testing.T) {
  54. i := build.Env{map[string]string{
  55. "fooname": "foovalue",
  56. "barname": "barvalue",
  57. "quxname": "quxvalue",
  58. }}
  59. assert.Equal(t, []string{
  60. `barname="barvalue"`,
  61. `fooname="foovalue"`,
  62. `quxname="quxvalue"`,
  63. }, i.Compile())
  64. }
  65. func TestLabel(t *testing.T) {
  66. i := build.Label{map[string]string{
  67. "fooname": "foovalue",
  68. "barname": "barvalue",
  69. "quxname": "quxvalue",
  70. }}
  71. assert.Equal(t, []string{
  72. `barname="barvalue"`,
  73. `fooname="foovalue"`,
  74. `quxname="quxvalue"`,
  75. }, i.Compile())
  76. }
  77. func TestUser(t *testing.T) {
  78. i := build.User{"foo"}
  79. assert.Equal(t, []string{`"foo"`}, i.Compile())
  80. }
  81. func TestWorkingDirectory(t *testing.T) {
  82. i := build.WorkingDirectory{"/foo/path"}
  83. assert.Equal(t, []string{`"/foo/path"`}, i.Compile())
  84. }