Skip to content

Commit

Permalink
add test for magnet download file stage
Browse files Browse the repository at this point in the history
  • Loading branch information
sarp committed Sep 10, 2024
1 parent 704b42d commit 3977940
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions internal/stage_magnet_dl_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package internal

import (
"os"
"path"

"github.com/codecrafters-io/tester-utils/test_case_harness"
)

func testMagnetDownloadFile(stageHarness *test_case_harness.TestCaseHarness) error {
initRandom()

logger := stageHarness.Logger
executable := stageHarness.Executable

t := randomMagnetLink()

tempDir, err := os.MkdirTemp("", "torrents")
if err != nil {
logger.Errorln("Couldn't create temp directory")
return err
}
downloadedFilePath := path.Join(tempDir, t.Filename)
magnetUrl := "magnet:?xt=urn:btih:" + t.InfoHashStr + "&dn=" + t.Filename + "&tr=http%3A%2F%2Fbittorrent-test-tracker.codecrafters.io%2Fannounce"

logger.Infof("Running ./your_bittorrent.sh magnet_download -o %s %q", downloadedFilePath, magnetUrl)
result, err := executable.Run("magnet_download", "-o", downloadedFilePath, magnetUrl)
if err != nil {
return err
}

if err = assertExitCode(result, 0); err != nil {
return err
}

if err = assertFileSize(downloadedFilePath, int64(t.FileLengthBytes)); err != nil {
return err
}

if err = assertFileSHA1(downloadedFilePath, t.ExpectedSha1); err != nil {
return err
}

return nil
}

0 comments on commit 3977940

Please sign in to comment.