From c4ae2d1b408a32149fae8f44a8553a9585608d78 Mon Sep 17 00:00:00 2001 From: jgstew Date: Sun, 24 Oct 2021 13:33:17 -0400 Subject: [PATCH] improve output and tests --- .github/workflows/test_build.yaml | 4 +++ src/bescli/bescli.py | 12 +++---- tests/tests.py | 54 +++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test_build.yaml b/.github/workflows/test_build.yaml index c3b9b8f..01619e2 100644 --- a/.github/workflows/test_build.yaml +++ b/.github/workflows/test_build.yaml @@ -32,6 +32,8 @@ jobs: python-version: 3.8 - name: Install requirements run: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Run Tests - Source + run: python tests/tests.py - name: Install build tools run: pip install setuptools wheel build - name: Run build @@ -52,3 +54,5 @@ jobs: - name: Test python bescli shell: bash run: python -m bescli ls logout clear error_count exit + - name: Run Tests - Pip + run: python tests/tests.py --test_pip diff --git a/src/bescli/bescli.py b/src/bescli/bescli.py index 588406d..52aeb5a 100644 --- a/src/bescli/bescli.py +++ b/src/bescli/bescli.py @@ -200,7 +200,7 @@ def do_login(self, user=None): else: self.perror("Login Error!") - def do_logout(self, arg): + def do_logout(self, arg=None): """Logout and clear session""" if self.bes_conn: self.bes_conn.logout() @@ -270,7 +270,7 @@ def do_ls(self, arg=None): def do_error_count(self, arg=None): """Output the number of errors""" - self.poutput(self.num_errors) + self.poutput(f"Error Count: {self.num_errors}") def do_exit(self, arg=None): """Exit this application""" @@ -288,13 +288,13 @@ def do_query(self, statement): if statement.raw: # get everything after `query ` rel_text = statement.raw.split(" ", 1)[1] - self.poutput("Q: " + rel_text) + self.poutput(f"Q: {rel_text}") rel_result = self.bes_conn.session_relevance_string(rel_text) - self.poutput(rel_result) + self.poutput(f"A: {rel_result}") - def do_version(self, statement): + def do_version(self, statement=None): """output version of besapi""" - print(__version__) + self.poutput(f"besapi version: {__version__}") def do_export_site(self, site_path): """export site contents to current folder""" diff --git a/tests/tests.py b/tests/tests.py index 56b0345..954ebbf 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1,19 +1,55 @@ +""" +Test besapi +""" +import argparse import os import subprocess +import sys -# set working directory to folder this file is in: -os.chdir(os.path.dirname(os.path.abspath(__file__))) +# check for --test_pip arg +parser = argparse.ArgumentParser() +parser.add_argument( + "--test_pip", help="to test package installed with pip", action="store_true" +) +args = parser.parse_args() -# set working directory to src folder in parent folder -os.chdir("../src") +if not args.test_pip: + # add module folder to import paths for testing local src + sys.path.append( + os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "src") + ) + # reverse the order so we make sure to get the local src module + sys.path.reverse() import besapi import bescli + print(f"besapi version: {besapi.__version__}") -if os.name == "nt": - subprocess.run( - 'CMD /C python -m besapi ls clear ls conf "query number of bes computers" exit', - check=True, - ) +bigfix_cli = bescli.bescli.BESCLInterface() + +# just make sure these don't throw errors: +bigfix_cli.do_ls() +bigfix_cli.do_clear() +bigfix_cli.do_ls() +bigfix_cli.do_logout() +bigfix_cli.do_error_count() +bigfix_cli.do_version() +bigfix_cli.do_conf() + +# this should really only run if the config file is present: +if bigfix_cli.bes_conn: + print(bigfix_cli.bes_conn.session_relevance_string("number of bes computers")) + + # set working directory to folder this file is in: + os.chdir(os.path.dirname(os.path.abspath(__file__))) + + # set working directory to src folder in parent folder + os.chdir("../src") + + if os.name == "nt": + subprocess.run( + 'CMD /C python -m besapi ls clear ls conf "query number of bes computers" version error_count exit', + check=True, + )