Skip to content

Commit

Permalink
Fix command line tool name
Browse files Browse the repository at this point in the history
See #33
  • Loading branch information
chrisn committed Nov 4, 2023
1 parent 764d291 commit 08ede78
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 37 deletions.
12 changes: 7 additions & 5 deletions bin/audio-offset-finder → audio_offset_finder/cli.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/python3

# audio-offset-finder
#
# Copyright (c) 2014-22 British Broadcasting Corporation
# Copyright (c) 2014-23 British Broadcasting Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from audio_offset_finder.audio_offset_finder import find_offset_between_files
from .audio_offset_finder import find_offset_between_files
import argparse
import sys

Expand Down Expand Up @@ -117,5 +115,9 @@ def plot_results(args, results):
pyplot.show()


if __name__ == "__main__":
def run():
sys.exit(main(sys.argv[1:]))


if __name__ == "__main__":
run()
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ dev =

[options.entry_points]
console_scripts =
executable-name = audio_offset_finder.audio_offset_finder:main
audio-offset-finder = audio_offset_finder.cli:run
39 changes: 8 additions & 31 deletions tests/tool_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# audio-offset-finder
#
# Copyright (c) 2014-22 British Broadcasting Corporation
# Copyright (c) 2014-23 British Broadcasting Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,41 +18,18 @@
import numpy as np
import os
import types
from importlib.machinery import ModuleSpec, SourceFileLoader
from importlib.util import spec_from_loader, module_from_spec
from audio_offset_finder.cli import main, reorder_correlations
from unittest.mock import patch
from io import StringIO
import tempfile


# Function to import code from a file
def import_from_source(name: str, file_path: str) -> types.ModuleType:
loader: SourceFileLoader = SourceFileLoader(name, file_path)
spec: ModuleSpec = spec_from_loader(loader.name, loader)
module: types.ModuleType = module_from_spec(spec)
loader.exec_module(module)
return module


script_path: str = os.path.abspath(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..",
"bin",
"audio-offset-finder",
)
)


tool: types.ModuleType = import_from_source("audio-offset-finder", script_path)


def test_reorder_correlations():
input_array1 = np.array([0, 1, 2, 3])
np.testing.assert_array_equal(tool.reorder_correlations(input_array1), np.array([2, 3, 0, 1]))
np.testing.assert_array_equal(reorder_correlations(input_array1), np.array([2, 3, 0, 1]))

input_array2 = np.array([0, 1, 2, 3, 4])
np.testing.assert_array_equal(tool.reorder_correlations(input_array2), np.array([2, 3, 4, 0, 1]))
np.testing.assert_array_equal(reorder_correlations(input_array2), np.array([2, 3, 4, 0, 1]))


def test_tool():
Expand All @@ -62,7 +39,7 @@ def test_tool():
"--find-offset-of tests/audio/timbl_2.mp3 --within tests/audio/timbl_1.mp3 --resolution 160 " "--trim 35 --save-plot "
) + plot_file_path
with patch("sys.stdout", new=StringIO()) as fakeStdout:
tool.main(args1.split())
main(args1.split())
output = fakeStdout.getvalue().strip()
assert output, "audio_offset_finder did not produce any output"
assert "Offset: 12.26" in output
Expand All @@ -72,13 +49,13 @@ def test_tool():

args2 = "--find-offset-of tests/audio/timbl_2.mp3"
with pytest.raises(SystemExit) as error:
tool.main(args2.split())
main(args2.split())
assert error.type == SystemExit
assert error.value.code > 0, "missing 'within' file"

args3 = "--within tests/audio/timbl_1.mp3"
with pytest.raises(SystemExit) as error:
tool.main(args3.split())
main(args3.split())
assert error.type == SystemExit
assert error.value.code > 0, "missing 'offset-of' file"

Expand All @@ -88,7 +65,7 @@ def test_json():

args = "--find-offset-of tests/audio/timbl_2.mp3 --within tests/audio/timbl_1.mp3 --resolution 160 " "--trim 35 --json"
with patch("sys.stdout", new=StringIO()) as fakeStdout:
tool.main(args.split())
main(args.split())
output = fakeStdout.getvalue().strip()
json_array = json.loads(output)
assert len(json_array) == 2
Expand Down

0 comments on commit 08ede78

Please sign in to comment.