Skip to content

feat: add template checkout command to extract a template to an expanded local directory instead of a tar #2867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Prev Previous commit
Next Next commit
add a test for pull into a directory
  • Loading branch information
ketang committed Jul 7, 2022
commit a42b278e8f5f5c02408f533dd9fcd79155a16c93
89 changes: 89 additions & 0 deletions cli/templatepull_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cli_test

import (
"archive/tar"
"bufio"
"bytes"
"fmt"
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/cli"
)

func writeTarArchiveFileEntry(tw *tar.Writer, filename string, content []byte) error {
hdr := &tar.Header{
Name: filename,
Mode: 0600,
Size: int64(len(content)),
}

err := tw.WriteHeader(hdr)
if err != nil {
return err
}
_, err = tw.Write([]byte(content))
if err != nil {
return err
}
return nil
}

func TestTemplatePullExtractArchive(t *testing.T) {
t.Parallel()

t.Run("TestPullExtractArchive", func(t *testing.T) {
subdirName := "subtle"
expectedNames := []string{
"rat-one", "rat-two", fmt.Sprintf("%s/trouble", subdirName),
}
expectedContents := []string{
"{ 'tar' : 'but is it art?' }\n", "{ 'zap' : 'brannigan' }\n", "{ 'with' : 'a T' }\n",
}

t.Parallel()

var bb bytes.Buffer
w := bufio.NewWriter(&bb)
tw := tar.NewWriter(w)

hdr := &tar.Header{
Name: subdirName,
Mode: 0700,
Typeflag: tar.TypeDir,
}
err := tw.WriteHeader(hdr)
if err != nil {
t.Fatalf(err.Error())
}

for i := 0; i < len(expectedNames); i++ {
err = writeTarArchiveFileEntry(tw, expectedNames[i], []byte(expectedContents[i]))
if err != nil {
t.Fatalf(err.Error())
}
}

tw.Close()

dirname, err := ioutil.TempDir("", "template-pull-test")
if err != nil {
t.Fatalf(err.Error())
}

cli.TarBytesToTree(dirname, bb.Bytes())

for i := 0; i < len(expectedNames); i++ {
filename := fmt.Sprintf("%s/%s", dirname, expectedNames[i])
actualContents, err := ioutil.ReadFile(filename)

if err != nil {
t.Fatalf(err.Error())
}

require.Equal(t, expectedContents[i], string(actualContents))
}
})
}