Skip to content

Commit

Permalink
show folder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andgineer committed Apr 15, 2019
1 parent 44aaefa commit 72526e2
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 42 deletions.
3 changes: 2 additions & 1 deletion bombard/expand_file_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def show_folder(folder_path):
print(name)
else:
print(f'\n{folder_path}:\n')
print(markdown_for_terminal(open(file_name, 'r').read()))
with open(file_name, 'r') as desc:
print(markdown_for_terminal(desc.read()))


61 changes: 30 additions & 31 deletions bombard/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from bombard.campaign_yaml import yaml
from bombard.bombardier import Bombardier
import logging
from bombard.args import get_args, CAMPAIGN_FILE_NAME, INIT_EXAMPLE
from bombard.args import get_args, CAMPAIGN_FILE_NAME, INIT_EXAMPLE, EXAMPLES_PREFIX
from typing import Optional
from bombard.request_logging import setup_logging, log
import os.path
from shutil import copyfile
from bombard.expand_file_name import get_campaign_file_name, show_folder, expand_relative_file_name
import sys
from bombard.terminal_colours import red
from bombard.terminal_colours import red, RED, OFF


def guess_type(value: str):
Expand Down Expand Up @@ -64,47 +64,23 @@ def init(args):
Copies the example to current folder as bombard.yaml
"""
if args.example is None:
src = expand_relative_file_name(f'bombard://{INIT_EXAMPLE}')
src = expand_relative_file_name(f'{EXAMPLES_PREFIX}{INIT_EXAMPLE}')
else:
src = get_campaign_file_name(args) # actually it will be from ==example
if os.path.isfile(CAMPAIGN_FILE_NAME):
log.error(f'File {CAMPAIGN_FILE_NAME} already exists.')
exit(1)
print(f'Cannot init from {src}:\n{RED}File {CAMPAIGN_FILE_NAME} already exists.{OFF}')
return
copyfile(src, CAMPAIGN_FILE_NAME)
#todo copy external python scripts if it is included into the example (create yaml CopyLoader)


def campaign(args):
if args.quiet:
level = logging.WARNING
elif args.verbose:
level = logging.DEBUG
else:
level = logging.INFO

setup_logging(level=level, log_file_name=args.log)

if args.init:
init(args)
exit(0)

def start_campaign(args, campaign_file_name):
log.debug(f'Starting bombard campaign with args\n' + ' '*4 + f'{args.__dict__}')
campaign_file_name = get_campaign_file_name(args)

if os.path.isdir(campaign_file_name):
show_folder(campaign_file_name)
exit(0)

supply = get_supply_from_cli(args.supply)

if not os.path.isfile(campaign_file_name) and not args.init:
print(red(f'\nCannot find campaign file "{args.file_name}"\n'))
args.parser.print_help(sys.stderr)
exit(1)

campaign_book = yaml.load(open(campaign_file_name, 'r'))
log.debug(f'Loaded bombard campaign from "{args.file_name}": {len(campaign_book["ammo"])} ammo.')

supply = get_supply_from_cli(args.supply)
load_book_supply(supply, campaign_book.get('supply', {}))
log.debug(f'Supply: {supply}')

Expand All @@ -122,6 +98,29 @@ def campaign(args):
bombardier.bombard()


def campaign(args):
if args.quiet:
level = logging.WARNING
elif args.verbose:
level = logging.DEBUG
else:
level = logging.INFO

setup_logging(level=level, log_file_name=args.log)

if args.init:
init(args)
return

campaign_file_name = get_campaign_file_name(args)
if os.path.isdir(campaign_file_name):
show_folder(campaign_file_name)
elif not os.path.isfile(campaign_file_name) and not args.init:
print(red(f'\nCannot find campaign file "{args.file_name}"\n'))
else:
start_campaign(args, campaign_file_name)


def main():
campaign(get_args())

Expand Down
2 changes: 1 addition & 1 deletion bombard/mock_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
All strings with `bombard.examples.mock_globals` will be automatically removed before running bombard scripts.
"""
from bombard.campaign_yaml import yaml
from bombard.args import expand_relative_file_name
from bombard.expand_file_name import expand_relative_file_name
import os.path # we use it to simplify import lines in examples


Expand Down
14 changes: 14 additions & 0 deletions bombard/tests/fake_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from bombard.attr_dict import AttrDict


class FakeArgs(AttrDict):
threads = 1
timeout = 3
ms = False
threshold = 1000
quiet = False
dry = False
verbose = False
quiete = False
log = None
example = None
14 changes: 14 additions & 0 deletions bombard/tests/stdout_contextmanager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import sys
from contextlib import contextmanager
from io import StringIO


class CaptureOutput:
def __enter__(self):
self.out, self.err = StringIO(), StringIO()
self.old_out, self.old_err = sys.stdout, sys.stderr
sys.stdout, sys.stderr = self.out, self.err
return self

def __exit__(self, *args):
sys.stdout, sys.stderr = self.old_out, self.old_err
10 changes: 1 addition & 9 deletions bombard/tests/test_http_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from bombard import http_request
from bombard.main import setup_logging
import logging
from tests.fake_args import FakeArgs


TEST_AMMO = {
Expand All @@ -18,15 +19,6 @@
}


class FakeArgs:
threads = 1
timeout = 3
ms = False
threshold = 1000
quiet = False
dry = False


class TestHttpRequests(unittest.TestCase):
def setUp(self):
http_request.http.client.HTTPSConnection.request = MagicMock()
Expand Down
24 changes: 24 additions & 0 deletions bombard/tests/test_show_folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from tests.stdout_contextmanager import CaptureOutput
import unittest
from bombard.main import campaign
from tests.fake_args import FakeArgs
from bombard.args import INIT_EXAMPLE, DIR_DESC_FILE_NAME, CAMPAIGN_FILE_NAME
import os.path


class TestShowFolder(unittest.TestCase):
def setUp(self):
if os.path.isfile(CAMPAIGN_FILE_NAME):
os.remove(CAMPAIGN_FILE_NAME)

def testShowFolder(self):
with CaptureOutput() as captured:
campaign(FakeArgs(init=True))
with open(f'bombard/examples/{DIR_DESC_FILE_NAME}') as desc:
expected = desc.read()
self.maxDiff = 1024
self.assertEqual(
captured.out.getvalue().strip(),
expected
)

0 comments on commit 72526e2

Please sign in to comment.