Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for dysh shell #125

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/dysh/shell.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argparse
from pathlib import Path
from typing import List
from typing import List, Union

import IPython
from traitlets.config import Config
Expand All @@ -14,7 +14,7 @@
Welcome to Dysh v{__version__}

Example usage: https://dysh.readthedocs.io/en/latest/example.html
Bug repots: https://github.com/GreenBankObservatory/dysh/issues
Bug reports: https://github.com/GreenBankObservatory/dysh/issues

For help with a Dysh routine from the command line,
use the builtin 'help'. e.g.:
Expand All @@ -28,6 +28,9 @@
--------------------------------------------------------------------------
"""

DEFAULT_PROFILE = "dysh"
DEFAULT_COLORS = "LightBG"


def parse_args():
parser = argparse.ArgumentParser(
Expand All @@ -37,15 +40,18 @@ def parse_args():
)
)
parser.add_argument("paths", help="FITS file paths to load initially", nargs="*", type=Path)
parser.add_argument("-p", "--profile", help="The IPython profile to use", default="dysh")
parser.add_argument("-p", "--profile", help="The IPython profile to use", default=DEFAULT_PROFILE)
parser.add_argument("-L", "--fits-loader", help="The SDFITS loader class name to use", default="GBTFITSLoad")
parser.add_argument(
"--colors", help="Set the color scheme", choices=["NoColor", "Neutral", "Linux", "LightBG"], default="LightBG"
"--colors",
help="Set the color scheme",
choices=["NoColor", "Neutral", "Linux", "LightBG"],
default=DEFAULT_COLORS,
)
return parser.parse_known_args()


def init_shell(colors: str, profile: str | Path, *ipython_args, sdfits_files=None):
def init_shell(*ipython_args, colors=DEFAULT_COLORS, profile: Union[str, Path] = "DEFAULT_PROFILE", sdfits_files=None):
c = Config()
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -81,7 +87,7 @@ def open_sdfits_files(paths: List[Path], loader_class_name="GBTFITSLoad") -> Lis
def main():
args, remaining_args = parse_args()
sdfits_files = open_sdfits_files(args.paths, args.fits_loader)
init_shell(colors=args.colors, profile=args.profile, *remaining_args, sdfits_files=sdfits_files)
init_shell(*remaining_args, colors=args.colors, profile=args.profile, sdfits_files=sdfits_files)


if __name__ == "__main__":
Expand Down
32 changes: 32 additions & 0 deletions src/dysh/test_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import subprocess
import sys
import uuid
from io import StringIO

from dysh.shell import init_shell


def test_shell_cli():
"""Simply prove that we can launch $ dysh from CLI"""
subprocess.check_call(["dysh"])


def test_shell_cli_with_args():
"""Simply prove that we can launch $ dysh from CLI"""
subprocess.check_call(["dysh", "--colors", "Linux", "--no-banner", "--profile", "foo"])


def test_init_shell(monkeypatch):
"""Prove that we can open the shell, print something, and exit without error"""

# Generate a unique string that we will look for in stdout later
test_str = uuid.uuid4().hex.upper()
mock_input = StringIO(f"print('{test_str}')\n")
monkeypatch.setattr(sys, "stdin", mock_input)

mock_stdout = StringIO()
monkeypatch.setattr(sys, "stdout", mock_stdout)
init_shell("--no-banner", colors="NoColor", profile="dysh")

mock_stdout.seek(0)
assert test_str in mock_stdout.read()