Skip to content

Commit

Permalink
refactor test_complete.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Obijuan committed Feb 24, 2024
1 parent 7f43b38 commit 79d6ca4
Showing 1 changed file with 111 additions and 37 deletions.
148 changes: 111 additions & 37 deletions test/packages/test_complete.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pytest
"""
Test different "apio" commands
"""

from os import getcwd, listdir, mkdir
from os.path import join, isfile, isdir, getsize
import pathlib

import click
import pytest

from apio.commands.install import cli as cmd_install
from apio.commands.uninstall import cli as cmd_uninstall
Expand All @@ -12,135 +13,208 @@
from apio.commands.examples import cli as cmd_examples


def validate_files_leds(apioproject_dir):
path = join(apioproject_dir, 'leds.v')
assert isfile(path) and getsize(path) > 0
def validate_files_leds(folder):
"""Check that the leds.v file is inside the given folder"""

# -- File to check
leds = folder / pathlib.Path('leds.v')

# -- The file should exists and have a size greather than 0
assert leds.exists() and leds.stat().st_size > 0 #getsize(leds) > 0


def validate_dir_leds(folder=""):
"""Check that the leds folder has been created in the
dir directory
"""

def validate_dir_leds(apioproject_dir):
path = join(apioproject_dir, 'leds')
assert isdir(path) and len(listdir(path)) > 0
#-- Get the leds path
leds_dir = folder / pathlib.Path("leds")

# -- Calculate the numer of files in the leds folder
nfiles = len(list(leds_dir.glob('*')))

# -- The folder should exist, and it should
# -- containe more than 0 files inside
assert leds_dir.is_dir() and nfiles > 0


def test_complete(clirunner, validate_cliresult, configenv, offline):
"""Test the installation of the examples package"""

# -- If the option 'offline' is passed, the test is skip
# -- (These tests require internet)
if offline:
pytest.skip('requires internet connection')

with clirunner.isolated_filesystem():

# -- Config the environment (conftest.configenv())
configenv()

# apio uninstall examples
# -- Execute "apio examples"
result = clirunner.invoke(
cmd_uninstall, ['examples'], input='y')
assert 'Do you want to continue?' in result.output
assert 'Error: package \'examples\' is not installed' in result.output

# apio install examples@X
# -- Execute "apio examples@X"
result = clirunner.invoke(cmd_install, ['examples@X'])
assert 'Error: package not availabe' in result.output
assert 'for this platform' in result.output

# apio install [email protected].7
result = clirunner.invoke(cmd_install, ['[email protected].7'])
# -- Execute "apio [email protected].34"
result = clirunner.invoke(cmd_install, ['[email protected].34'])
validate_cliresult(result)
assert 'Installing examples package' in result.output
assert 'Download' in result.output
assert 'has been successfully installed!' in result.output

# apio install examples
# -- Execute "apio install examples"
result = clirunner.invoke(cmd_install, ['examples'])
validate_cliresult(result)
assert 'Installing examples package' in result.output
assert 'Download' in result.output
assert 'has been successfully installed!' in result.output

# apio install examples
# -- Execute "apio install examples" again
result = clirunner.invoke(cmd_install, ['examples'])
validate_cliresult(result)
assert 'Installing examples package' in result.output
assert 'Already installed. Version ' in result.output

# apio install examples -p windows
# -- Execute "apio install examples --platform windows --force"
result = clirunner.invoke(cmd_install, [
'examples', '--platform', 'windows', '--force'])
validate_cliresult(result)
assert 'Installing examples package' in result.output
assert 'Download' in result.output
assert 'has been successfully installed!' in result.output

# apio install --list
# -- Execute "apio install --list"
result = clirunner.invoke(cmd_install, ['--list'])
validate_cliresult(result)
assert 'Installed packages:' in result.output
assert 'examples' in result.output


# apio init --board icezum
result = clirunner.invoke(cmd_init, ['--board', 'icezum'])
def test_complete2(clirunner, validate_cliresult, configenv, offline):
"""Test more 'apio examples' commands """

# -- If the option 'offline' is passed, the test is skip
# -- (These tests require internet)
if offline:
pytest.skip('requires internet connection')

with clirunner.isolated_filesystem():

# -- Config the environment (conftest.configenv())
configenv()

# -- Execute "apio init --board alhambra-ii"
result = clirunner.invoke(cmd_init, ['--board', 'alhambra-ii'])
validate_cliresult(result)
assert 'Creating apio.ini file ...' in result.output
assert 'has been successfully created!' in result.output

# apio upload
# -- Execute "apio upload"
result = clirunner.invoke(cmd_upload)
assert result.exit_code == 1
assert "package 'oss-cad-suite' is not installed" in result.output

# -- Execute "apio install examples"
result = clirunner.invoke(cmd_install, ['examples'])
validate_cliresult(result)
assert 'Installing examples package' in result.output
assert 'Download' in result.output
assert 'has been successfully installed!' in result.output

# apio examples --list
# -- Execute "apio examples --list"
result = clirunner.invoke(cmd_examples, ['--list'])
validate_cliresult(result)
assert 'leds' in result.output
assert 'icezum' in result.output

# apio examples --files missing_example
# -- Execute "apio examples --files missing_example"
result = clirunner.invoke(cmd_examples, ['--files', 'missing_example'])
validate_cliresult(result)
assert 'Warning: this example does not exist' in result.output

# apio examples --files leds
# -- Execute "apio examples --files leds"
result = clirunner.invoke(cmd_examples, ['--files', 'leds'])
validate_cliresult(result)
assert 'Copying leds example files ...' in result.output
assert 'have been successfully created!' in result.output
validate_files_leds(getcwd())
validate_files_leds(pathlib.Path())

# apio examples --dir leds
# -- Execute "apio examples --dir leds"
result = clirunner.invoke(cmd_examples, ['--dir', 'leds'])
validate_cliresult(result)
assert 'Creating leds directory ...' in result.output
assert 'has been successfully created!' in result.output
validate_dir_leds(getcwd())
validate_dir_leds()

# apio examples --dir leds
# -- Execute "apio examples --dir leds"
result = clirunner.invoke(cmd_examples, ['--dir', 'leds'], input='y')
validate_cliresult(result)
assert 'Warning: leds directory already exists' in result.output
assert 'Do you want to replace it?' in result.output
assert 'Creating leds directory ...' in result.output
assert 'has been successfully created!' in result.output
validate_dir_leds(getcwd())
validate_dir_leds()


def test_complete3(clirunner, validate_cliresult, configenv, offline):
"""Test more 'apio examples' commands """

# -- If the option 'offline' is passed, the test is skip
# -- (These tests require internet)
if offline:
pytest.skip('requires internet connection')

with clirunner.isolated_filesystem():

# -- Config the environment (conftest.configenv())
configenv()

# -- Execute "apio install examples"
result = clirunner.invoke(cmd_install, ['examples'])
validate_cliresult(result)
assert 'Installing examples package' in result.output
assert 'Download' in result.output
assert 'has been successfully installed!' in result.output

dir_name = 'tmp'
mkdir(dir_name)
# ------------------------------------------
# -- Check the --project-dir parameter
# ------------------------------------------
#-- Create a tmp dir
p = pathlib.Path("tmp/")
p.mkdir(parents=True, exist_ok=True)

# apio examples --files leds --project-dir=tmp
# -- Execute "apio examples --files leds --project-dir=tmp"
result = clirunner.invoke(
cmd_examples, ['--files', 'leds', '--project-dir=tmp'])
validate_cliresult(result)
assert 'Copying leds example files ...' in result.output
assert 'have been successfully created!' in result.output
validate_files_leds(join(getcwd(), dir_name))

# apio examples --dir leds --project-dir=tmp
# -- Check the files in the tmp folder
validate_files_leds(p)

# -- Execute "apio examples --dir leds --project-dir=tmp"
result = clirunner.invoke(
cmd_examples, ['--dir', 'leds', '--project-dir=tmp'])
validate_cliresult(result)
assert 'Creating leds directory ...' in result.output
assert 'has been successfully created!' in result.output
validate_dir_leds(join(getcwd(), dir_name))
validate_dir_leds('tmp')

# apio uninstall examples
# -- Execute "apio uninstall examples"
result = clirunner.invoke(cmd_uninstall, ['examples'], input='n')
validate_cliresult(result)
assert 'Abort!' in result.output

# apio uninstall examples
# -- Execute "apio uninstall examples"
result = clirunner.invoke(cmd_uninstall, ['examples'], input='y')
validate_cliresult(result)
assert 'Uninstalling examples package' in result.output
Expand Down

0 comments on commit 79d6ca4

Please sign in to comment.