from nose.tools import istest, assert_equal

from whack.downloads import read_downloads_string, Download


@istest
def filename_of_download_uses_url_if_not_explicitly_set():
    download = Download("http://nginx.org/download/nginx-1.2.6.tar.gz")
    assert_equal("nginx-1.2.6.tar.gz", download.filename)

@istest
def empty_plain_text_file_has_no_downloads():
    assert_equal([], read_downloads_string(""))
    
@istest
def blank_lines_in_downloads_file_are_ignored():
    assert_equal([], read_downloads_string("\n\n   \n\n"))
    
@istest
def each_line_of_downloads_file_is_url_of_download():
    assert_equal(
        [Download("http://nginx.org/download/nginx-1.2.6.tar.gz")],
        read_downloads_string("http://nginx.org/download/nginx-1.2.6.tar.gz")
    )

@istest
def filename_can_be_explicitly_set_after_url():
    assert_equal(
        [Download("http://nginx.org/download/nginx-1.2.6.tar.gz", "nginx.tar.gz")],
        read_downloads_string("http://nginx.org/download/nginx-1.2.6.tar.gz nginx.tar.gz")
    )

@@ -1,12 +1,23 @@
+import os%0A%0A
 from nose.to
@@ -47,16 +47,53 @@
 rt_equal
+%0Afrom catchy import NoCachingStrategy
 %0A%0Afrom w
@@ -145,16 +145,124 @@
 Download
+, Downloader%0Afrom whack import files%0Afrom whack.tempdir import create_temporary_dir%0Afrom . import httpserver
 %0A%0A%0A@iste
@@ -1154,24 +1154,649 @@
 gz nginx.tar.gz%22)%0A    )%0A
+%0A%0A@istest%0Adef downloader_can_download_files_over_http():%0A    downloader = Downloader(NoCachingStrategy())%0A    %0A    with create_temporary_dir() as server_root:%0A        files.write_file(os.path.join(server_root, %22hello%22), %22Hello there!%22)%0A        with httpserver.start_static_http_server(server_root) as http_server:%0A            with create_temporary_dir() as download_dir:%0A                download_path = os.path.join(download_dir, %22file%22)%0A                url = http_server.static_url(%22hello%22)%0A                downloader.download(url, download_path)%0A                assert_equal(%22Hello there!%22, files.read_file(download_path))%0A

Add test for Downloader.download