# Path: tests/conftest.py
# def call_cmd(cmd, expected_exit_code=0):
#     proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
#     stdout, stderr = proc.communicate()
#     stdout = stdout.decode('utf-8').strip()
#     stderr = stderr.decode('utf-8').strip()
#     if proc.returncode != expected_exit_code:
#         print(stdout)
#         print(stderr)
#         assert proc.returncode == expected_exit_code
#     return (stdout, stderr)
# 
# NASA_EXPECTED_FILES = {
#     'globe_west_540.jpg',
#     'globe_west_540_thumb.jpg',
#     'nasa_archive.torrent',
#     'nasa_meta.sqlite',
#     'nasa_files.xml',
#     'nasa_meta.xml',
#     'nasa_reviews.xml',
#     'nasa_itemimage.jpg',
#     'globe_west_540_thumb.jpg',
#     '__ia_thumb.jpg',
# }
# 
# def files_downloaded(path):
#     found_files = set()
#     try:
#         found_files = set(os.listdir(path))
#     except (FileNotFoundError, WindowsError, OSError):
#         pass
#     return found_files


import sys
import os
import time
from tests.conftest import call_cmd, NASA_EXPECTED_FILES, files_downloaded


def test_checksum(tmpdir_ch):
    call_cmd('ia --insecure download nasa nasa_meta.xml')
    assert files_downloaded('nasa') == {'nasa_meta.xml'}

    stdout, stderr = call_cmd('ia --insecure download --checksum nasa nasa_meta.xml')
    assert files_downloaded('nasa') == {'nasa_meta.xml'}
    assert 'nasa:\n skipping nasa/nasa_meta.xml, file already exists based on checksum.' == stderr


def test_no_directories(tmpdir_ch):
    call_cmd('ia --insecure download --no-directories nasa nasa_meta.xml')
    assert files_downloaded('.') == {'nasa_meta.xml'}


def test_destdir(tmpdir_ch):
    cmd = 'ia --insecure download --destdir=thisdirdoesnotexist/ nasa nasa_meta.xml'
    stdout, stderr = call_cmd(cmd, expected_exit_code=1)

    assert '--destdir must be a valid path to a directory.' in stderr

    tmpdir_ch.mkdir('thisdirdoesnotexist/')
    call_cmd(cmd)
    assert files_downloaded('thisdirdoesnotexist/nasa') == {'nasa_meta.xml'}

    tmpdir_ch.mkdir('dir2/')
    cmd = ('ia --insecure download --no-directories --destdir=dir2/ '
           'nasa nasa_meta.xml')
    call_cmd(cmd)

# Path: tests/conftest.py
# def call_cmd(cmd, expected_exit_code=0):
#     proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
#     stdout, stderr = proc.communicate()
#     stdout = stdout.decode('utf-8').strip()
#     stderr = stderr.decode('utf-8').strip()
#     if proc.returncode != expected_exit_code:
#         print(stdout)
#         print(stderr)
#         assert proc.returncode == expected_exit_code
#     return (stdout, stderr)
# 
# NASA_EXPECTED_FILES = {
#     'globe_west_540.jpg',
#     'globe_west_540_thumb.jpg',
#     'nasa_archive.torrent',
#     'nasa_meta.sqlite',
#     'nasa_files.xml',
#     'nasa_meta.xml',
#     'nasa_reviews.xml',
#     'nasa_itemimage.jpg',
#     'globe_west_540_thumb.jpg',
#     '__ia_thumb.jpg',
# }
# 
# def files_downloaded(path):
#     found_files = set()
#     try:
#         found_files = set(os.listdir(path))
#     except (FileNotFoundError, WindowsError, OSError):
#         pass
#     return found_files


# Path: tests/cli/test_ia_download.py
import sys
import os
import time
from tests.conftest import call_cmd, NASA_EXPECTED_FILES, files_downloaded



def test_checksum(tmpdir_ch):
    call_cmd('ia --insecure download nasa nasa_meta.xml')
    assert files_downloaded('nasa') == {'nasa_meta.xml'}

    stdout, stderr = call_cmd('ia --insecure download --checksum nasa nasa_meta.xml')
    assert files_downloaded('nasa') == {'nasa_meta.xml'}
    assert 'nasa:\n skipping nasa/nasa_meta.xml, file already exists based on checksum.' == stderr


def test_no_directories(tmpdir_ch):
    call_cmd('ia --insecure download --no-directories nasa nasa_meta.xml')
    assert files_downloaded('.') == {'nasa_meta.xml'}


def test_destdir(tmpdir_ch):
    cmd = 'ia --insecure download --destdir=thisdirdoesnotexist/ nasa nasa_meta.xml'
    stdout, stderr = call_cmd(cmd, expected_exit_code=1)

    assert '--destdir must be a valid path to a directory.' in stderr

    tmpdir_ch.mkdir('thisdirdoesnotexist/')
    call_cmd(cmd)
    assert files_downloaded('thisdirdoesnotexist/nasa') == {'nasa_meta.xml'}

    tmpdir_ch.mkdir('dir2/')
    cmd = ('ia --insecure download --no-directories --destdir=dir2/ '
           'nasa nasa_meta.xml')
    call_cmd(cmd)

    assert files_downloaded('dir2') == {'nasa_meta.xml'}