Skip to content

Commit

Permalink
Fix how skool2bin.py reads configuration from skoolkit.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
skoolkid committed Sep 18, 2024
1 parent 6b31309 commit 89bbf26
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 11 deletions.
8 changes: 4 additions & 4 deletions skoolkit/skool2bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ def main(args):
parser.add_argument('skoolfile', help=argparse.SUPPRESS, nargs='?')
parser.add_argument('binfile', help=argparse.SUPPRESS, nargs='?')
group = parser.add_argument_group('Options')
group.add_argument('-B', '--banks', action='store_true',
group.add_argument('-B', '--banks', action='store_const', const=1, default=config['Banks'],
help="Process @bank directives and write RAM banks 0-7 to a 128K file.")
group.add_argument('-b', '--bfix', dest='fix_mode', action='store_const', const=2, default=0,
help="Apply @ofix and @bfix directives.")
group.add_argument('-d', '--data', dest='data', action='store_true',
group.add_argument('-d', '--data', action='store_const', const=1, default=config['Data'],
help="Process @defb, @defs and @defw directives.")
group.add_argument('-E', '--end', dest='end', metavar='ADDR', type=integer, default=65537,
help='Stop converting at this address.')
Expand All @@ -327,11 +327,11 @@ def main(args):
help="Apply @isub and @ssub directives.")
group.add_argument('-S', '--start', dest='start', metavar='ADDR', type=integer, default=-1,
help='Start converting at this address.')
group.add_argument('-v', '--verbose', action='store_true',
group.add_argument('-v', '--verbose', action='store_const', const=1, default=config['Verbose'],
help='Show info on each converted instruction.')
group.add_argument('-V', '--version', action='version', version='SkoolKit {}'.format(VERSION),
help='Show SkoolKit version number and exit.')
group.add_argument('-w', '--no-warnings', dest='warn', action='store_false',
group.add_argument('-w', '--no-warnings', dest='warn', action='store_const', const=0, default=config['Warnings'],
help="Suppress warnings.")
namespace, unknown_args = parser.parse_known_args(args)
if namespace.show_config:
Expand Down
110 changes: 103 additions & 7 deletions tests/test_skool2bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def write(self, binfile):
self.binfile = binfile

class Skool2BinTest(SkoolKitTestCase):
def _check_values(self, skoolfile, binfile, asm_mode=0, fix_mode=0, banks=False, data=False,
verbose=False, warn=True, start=-1, end=65537, pad_left=65536, pad_right=0):
def _check_values(self, skoolfile, binfile, asm_mode=0, fix_mode=0, banks=0, data=0,
verbose=0, warn=1, start=-1, end=65537, pad_left=65536, pad_right=0):
self.assertEqual(mock_bin_writer.skoolfile, skoolfile)
self.assertEqual(mock_bin_writer.binfile, binfile)
self.assertEqual(mock_bin_writer.asm_mode, asm_mode)
Expand Down Expand Up @@ -91,6 +91,54 @@ def test_output_filename(self):
self.assertEqual(len(error), 0)
self.assertEqual(mock_bin_writer.binfile, binfile)

@patch.object(skool2bin, 'get_config', mock_config)
@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_config_Banks_set_on_command_line(self):
skoolfile = 'test-Banks.skool'
exp_binfile = skoolfile[:-6] + '.bin'
for option, value in (('-I', 0), ('--ini', 1)):
output, error = self.run_skool2bin(f'{option} Banks={value} {skoolfile}')
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, banks=value)

@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_config_Banks_read_from_file(self):
skoolfile = 'test-Banks.skool'
exp_binfile = skoolfile[:-6] + '.bin'
for value in (0, 1):
ini = f"""
[skool2bin]
Banks={value}
"""
self.write_text_file(dedent(ini).strip(), 'skoolkit.ini')
output, error = self.run_skool2bin(skoolfile)
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, banks=value)

@patch.object(skool2bin, 'get_config', mock_config)
@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_config_Data_set_on_command_line(self):
skoolfile = 'test-Data.skool'
exp_binfile = skoolfile[:-6] + '.bin'
for option, value in (('-I', 0), ('--ini', 1)):
output, error = self.run_skool2bin(f'{option} Data={value} {skoolfile}')
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, data=value)

@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_config_Data_read_from_file(self):
skoolfile = 'test-Data.skool'
exp_binfile = skoolfile[:-6] + '.bin'
for value in (0, 1):
ini = f"""
[skool2bin]
Data={value}
"""
self.write_text_file(dedent(ini).strip(), 'skoolkit.ini')
output, error = self.run_skool2bin(skoolfile)
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, data=value)

@patch.object(skool2bin, 'get_config', mock_config)
@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_config_PadLeft_set_on_command_line(self):
Expand Down Expand Up @@ -120,7 +168,7 @@ def test_config_PadRight_set_on_command_line(self):
self._check_values(skoolfile, binfile, pad_right=65536)

@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_config_PadLeft_read_from_file(self):
def test_config_PadRight_read_from_file(self):
ini = """
[skool2bin]
PadRight=65536
Expand All @@ -131,14 +179,62 @@ def test_config_PadLeft_read_from_file(self):
self.run_skool2bin(f'{skoolfile} {binfile}')
self._check_values(skoolfile, binfile, pad_right=65536)

@patch.object(skool2bin, 'get_config', mock_config)
@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_config_Verbose_set_on_command_line(self):
skoolfile = 'test-Verbose.skool'
exp_binfile = skoolfile[:-6] + '.bin'
for option, value in (('-I', 0), ('--ini', 1)):
output, error = self.run_skool2bin(f'{option} Verbose={value} {skoolfile}')
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, verbose=value)

@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_config_Verbose_read_from_file(self):
skoolfile = 'test-Verbose.skool'
exp_binfile = skoolfile[:-6] + '.bin'
for value in (0, 1):
ini = f"""
[skool2bin]
Verbose={value}
"""
self.write_text_file(dedent(ini).strip(), 'skoolkit.ini')
output, error = self.run_skool2bin(skoolfile)
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, verbose=value)

@patch.object(skool2bin, 'get_config', mock_config)
@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_config_Warnings_set_on_command_line(self):
skoolfile = 'test-Warnings.skool'
exp_binfile = skoolfile[:-6] + '.bin'
for option, value in (('-I', 0), ('--ini', 1)):
output, error = self.run_skool2bin(f'{option} Warnings={value} {skoolfile}')
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, warn=value)

@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_config_Warnings_read_from_file(self):
skoolfile = 'test-Warnings.skool'
exp_binfile = skoolfile[:-6] + '.bin'
for value in (0, 1):
ini = f"""
[skool2bin]
Warnings={value}
"""
self.write_text_file(dedent(ini).strip(), 'skoolkit.ini')
output, error = self.run_skool2bin(skoolfile)
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, warn=value)

@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_option_B(self):
skoolfile = 'test-B.skool'
exp_binfile = skoolfile[:-6] + '.bin'
for option in ('-B', '--banks'):
output, error = self.run_skool2bin(f'{option} {skoolfile}')
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, banks=True)
self._check_values(skoolfile, exp_binfile, banks=1)

@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_option_b(self):
Expand All @@ -156,7 +252,7 @@ def test_option_d(self):
for option in ('-d', '--data'):
output, error = self.run_skool2bin('{} {}'.format(option, skoolfile))
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, data=True)
self._check_values(skoolfile, exp_binfile, data=1)

@patch.object(skool2bin, 'BinWriter', MockBinWriter)
def test_option_E(self):
Expand Down Expand Up @@ -311,7 +407,7 @@ def test_option_v(self):
for option in ('-v', '--verbose'):
output, error = self.run_skool2bin('{} {}'.format(option, skoolfile))
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, verbose=True)
self._check_values(skoolfile, exp_binfile, verbose=1)

def test_option_V(self):
for option in ('-V', '--version'):
Expand All @@ -325,7 +421,7 @@ def test_option_w(self):
for option in ('-w', '--no-warnings'):
output, error = self.run_skool2bin('{} {}'.format(option, skoolfile))
self.assertEqual(len(error), 0)
self._check_values(skoolfile, exp_binfile, warn=False)
self._check_values(skoolfile, exp_binfile, warn=0)

class BinWriterTestCase(SkoolKitTestCase):
def _test_write(self, skool, base_address, exp_data, *modes, banks=False, data=False, start=-1,
Expand Down

0 comments on commit 89bbf26

Please sign in to comment.