Skip to content

Commit 8ba8d5a

Browse files
committed
fix typo in upload size check
The scp upload size check had a typo preventing files from reporting their size, causing an extra temp file to be created.
1 parent be5984d commit 8ba8d5a

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

internal/communicator/ssh/communicator.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func (c *Communicator) Upload(path string, input io.Reader) error {
418418
switch src := input.(type) {
419419
case *os.File:
420420
fi, err := src.Stat()
421-
if err != nil {
421+
if err == nil {
422422
size = fi.Size()
423423
}
424424
case *bytes.Buffer:
@@ -641,7 +641,13 @@ func checkSCPStatus(r *bufio.Reader) error {
641641
return nil
642642
}
643643

644+
var testUploadSizeHook func(size int64)
645+
644646
func scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader, size int64) error {
647+
if testUploadSizeHook != nil {
648+
testUploadSizeHook(size)
649+
}
650+
645651
if size == 0 {
646652
// Create a temporary file where we can copy the contents of the src
647653
// so that we can determine the length, since SCP is length-prefixed.

internal/communicator/ssh/communicator_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,28 @@ func TestAccUploadFile(t *testing.T) {
577577
}
578578

579579
tmpDir := t.TempDir()
580+
source, err := os.CreateTemp(tmpDir, "tempfile.in")
581+
if err != nil {
582+
t.Fatal(err)
583+
}
584+
585+
content := "this is the file content"
586+
if _, err := source.WriteString(content); err != nil {
587+
t.Fatal(err)
588+
}
589+
source.Seek(0, io.SeekStart)
580590

581-
content := []byte("this is the file content")
582-
source := bytes.NewReader(content)
583591
tmpFile := filepath.Join(tmpDir, "tempFile.out")
592+
593+
testUploadSizeHook = func(size int64) {
594+
if size != int64(len(content)) {
595+
t.Errorf("expected %d bytes, got %d\n", len(content), size)
596+
}
597+
}
598+
defer func() {
599+
testUploadSizeHook = nil
600+
}()
601+
584602
err = c.Upload(tmpFile, source)
585603
if err != nil {
586604
t.Fatalf("error uploading file: %s", err)
@@ -591,7 +609,7 @@ func TestAccUploadFile(t *testing.T) {
591609
t.Fatal(err)
592610
}
593611

594-
if !bytes.Equal(data, content) {
612+
if string(data) != content {
595613
t.Fatalf("bad: %s", data)
596614
}
597615
}

0 commit comments

Comments
 (0)