Skip to content
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

added first pass testing #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
output
3 changes: 2 additions & 1 deletion filelist.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
https://blog.badgerops.net/content/images/2020/03/badger.png
https://blog.badgerops.net/content/images/2020/03/badger.png
https://www.bobrossquotes.com/bobs/bob.png
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ func main() {

// for each url in given file, download the given file
for _, url := range rawFile {
// download the content
fmt.Println(" check out the values! ", url)
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error downloading file:", url)
Expand Down
63 changes: 63 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"testing"
)

func TestMain(t *testing.T) {
if os.Getenv("BE_CRASHER") == "1" {
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "synctool_test")
if err != nil {
t.Fatal("Failed to create temporary directory:", err)
}
defer os.RemoveAll(tempDir)

// Create a temporary input file with URLs
tempFile, err := ioutil.TempFile("", "urls.txt")
if err != nil {
t.Fatal("Failed to create temporary file:", err)
}
defer tempFile.Close()

// Write some URLs to the temporary input file
urls := []string{"https://blog.badgerops.net/content/images/2020/03/badger.png", "https://www.bobrossquotes.com/bobs/bob.png"}
for _, url := range urls {
_, err := tempFile.WriteString(url + "\n")
if err != nil {
t.Fatal("Failed to write to temporary file:", err)
}
}

// Run the main function with the temporary input file and output directory
fmt.Println(("The temp file name is: " + tempFile.Name()))
os.Args = []string{"downloader", "-f", tempFile.Name(), "-o", "./synctool_test"}
main()

// Check if the files were downloaded and saved correctly
for _, url := range urls {
filePath := filepath.Join("./synctool_test", path.Base(url))
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Errorf("File not found: %s", filePath)
}
}
return
}
}
func TestOsExit(t *testing.T) {
if os.Getenv("BE_CRASHER") != "1" {
cmd := exec.Command(os.Args[0], "-test.run=TestOsExit")
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("Process ran with err %v, want os.Exit(1)", err)
}
}