{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "wAWQSv4bSpEm" }, "source": [ "# Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XVo0btyhMxC3", "outputId": "370b1ac3-0c9c-4c01-c8db-5c522fc8b1e0", "scrolled": false }, "outputs": [], "source": [ "try: \n", " import casatools\n", "except:\n", " !pip install casatools -i https://casa-pip.nrao.edu:443/repository/pypi-group/simple\n", " \n", "try:\n", " import casatasks\n", "except:\n", " !pip install casatasks -i https://casa-pip.nrao.edu:443/repository/pypi-group/simple\n", "\n", "try:\n", " import casatestutils\n", "except:\n", " !pip install casatestutils -i https://casa-pip.nrao.edu:443/repository/pypi-group/simple" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "9Zl1m275PZlF" }, "outputs": [], "source": [ "import os, sys\n", "import unittest\n", "import pytest\n", "import shutil" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings(action=\"ignore\", message=\"unclosed\", category=ResourceWarning)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "user_data_path = os.path.expanduser(\"~/.casa/measures\")\n", "exec_path = sys.executable\n", "IN_COLAB = 'google.colab' in sys.modules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "oRhvqEM9CHam", "outputId": "0151f130-c350-4e88-c5f4-ab51cec2f023" }, "outputs": [], "source": [ "cwd = os.getcwd()\n", "cwd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"In Google Colab: {}\".format(IN_COLAB))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wQ4b912PUGXD", "outputId": "1a7b572d-d885-435e-c939-e05b53f11a96" }, "outputs": [], "source": [ " if not os.path.exists(os.path.expanduser(\"~/.casa/measures\")):\n", " !{exec_path} -m casatools --update-user-data" ] }, { "cell_type": "markdown", "metadata": { "id": "DzPEAsdDjhKd" }, "source": [ "# Checkout Version of runtest.py to test" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8e33ZPl6XAU-", "outputId": "9d9cf925-9caf-45b0-d971-cfada58a1c63" }, "outputs": [], "source": [ "if IN_COLAB:\n", " %cd /content\n", " pass\n", "else:\n", " workpath = cwd + '/work'\n", " if os.path.exists(workpath):\n", " shutil.rmtree(workpath)\n", " os.makedirs(workpath)\n", " %cd {workpath}\n", "!pwd " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "mI6zmVJ5NKXD", "outputId": "9b63bb58-6347-48bc-afa5-f7491eec9cdb" }, "outputs": [], "source": [ "!git clone https://open-bitbucket.nrao.edu/scm/casa/casa6.git\n", " \n", "### Uncomment to Test Specific Version of Runtest From Branch\n", "#%cd casa6\n", "#!git checkout CAS-13789\n", "#if IN_COLAB:\n", "# %cd /content\n", "#else:\n", "# os.chdir(\"..\")" ] }, { "cell_type": "markdown", "metadata": { "id": "O9rOQkQ7m_UJ" }, "source": [ "## Sparse Checkout Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VopmBjwYnSZM", "outputId": "b9f27817-da22-4611-f888-314efc55d2a4" }, "outputs": [], "source": [ "if IN_COLAB:\n", " !sudo apt-get install git-lfs" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0pZrPGwDGMHa", "outputId": "3d4da0ca-ac0a-4994-bbce-a8d2562a0242" }, "outputs": [], "source": [ "if IN_COLAB: # Check to see if in Google Colab\n", " %cd /content/\n", " !rm -rf casatestdata/ \n", "elif os.path.exists(\"/home/casa/data/casatestdata/\"): # Try to default to /home/casa\n", " datapath = \"/home/casa/data/casatestdata/\"\n", "elif os.path.exists(os.path.join(os.path.expanduser('~'),\".casa/config.py\")): # Check for user config.py\n", " config_path = os.path.join(os.path.expanduser('~'),\".casa/config.py\")\n", " with open(config_path) as fp:\n", " lines = fp.readlines()\n", " for line in lines:\n", " if 'datapath' in line and not line.startswith('#'):\n", " x = re.search(r\"\\/(...)*\\/\", line)\n", " datapath = str(x.group())\n", " if not os.path.exists(datapath):\n", " datapath = \"/home/\"\n", " print(\"Datapath Unknown. Set Manually\")\n", " break\n", "else:\n", " datapath = \"/home/\"\n", " print(\"Datapath Unknown. Set Manually\")\n", "print(\"Datapath: {}\".format(datapath))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NjnaDx7CnmQV", "outputId": "706641de-b933-4f46-b19f-284fcfd7253f" }, "outputs": [], "source": [ "if IN_COLAB:\n", " !git clone --no-checkout https://open-bitbucket.nrao.edu/scm/casa/casatestdata.git\n", " %cd /content/casatestdata\n", " !git config core.sparseCheckout true\n", " !git config filter.lfs.clean \"git-lfs clean -- %f\"\n", " !git config filter.lfs.smudge \"git-lfs smudge -- %f\"\n", " !git config filter.lfs.process \"git-lfs filter-process\" \n", " !echo \"unittest/*\" >> .git/info/sparse-checkout \n", " #!echo \"image/*\" >> .git/info/sparse-checkout \n", " !echo \"measurementset/alma/nep2-shrunk.ms/*\" >> .git/info/sparse-checkout \n", " !git checkout master" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CFqaMi9FxGL9", "outputId": "745ebb37-1624-46d0-b5e4-0360c46a5ca9" }, "outputs": [], "source": [ "!git config --list " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Tg2pUqxfsceC", "outputId": "403a5ec0-2417-4c10-998e-ef2ffeea8d61" }, "outputs": [], "source": [ "!pwd\n", "!ls -alh" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "ieW-79gLqRq3" }, "outputs": [], "source": [ "if IN_COLAB:\n", " !rm -rf /root/.casa/config.py\n", " !touch /root/.casa/config.py\n", " !echo \"import time\" >> /root/.casa/config.py\n", " !echo \"import sys\" >> /root/.casa/config.py\n", " !echo \"datapath=[ '/content/casatestdata/']\" >> /root/.casa/config.py\n", " !cat /root/.casa/config.py\n", "else:\n", " config_path = os.path.expanduser(\"~/.casa/config.py\")\n", " if os.path.exists(config_path):\n", " !cat {config_path}\n", " else:\n", " !touch {config_path}\n", " !echo \"import time\" >> {config_path}\n", " !echo \"import sys\" >> {config_path}\n", " string = \"datapath=[ '{}']\".format(datapath)\n", " !echo {string} >> {config_path}" ] }, { "cell_type": "markdown", "metadata": { "id": "ZnmTMs6JjrUx" }, "source": [ "# Tests" ] }, { "cell_type": "markdown", "metadata": { "id": "4RcF1Ke5OXgf" }, "source": [ "## Test Case 1\n", "\n", "Test that runtest finds working path" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pwd" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "NPmdDP5fR5Fw" }, "outputs": [], "source": [ "class TestClass(unittest.TestCase):\n", " \n", " @classmethod\n", " def setUpClass(cls):\n", " !touch test_foo.py\n", "\n", " @classmethod\n", " def tearDownClass(cls):\n", " !rm -rf nosedir\n", " !rm -rf test_foo.py\n", "\n", " def test_nosedir_created(self):\n", " if IN_COLAB:\n", " !{exec_path} /content/casa6/casatestutils/runtest.py test_foo.py\n", " else:\n", " !{exec_path} {cwd}/work/casa6/casatestutils/runtest.py test_foo.py\n", " self.assertTrue(os.path.exists(\"nosedir/\"))\n" ] }, { "cell_type": "markdown", "metadata": { "id": "9vnNX3HpU1dZ" }, "source": [ "## Test Case 2\n", "\n", "Test that runtest creates test working directory " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZOrpEU3hT431", "outputId": "d8831939-b70f-4b26-9d91-787556a9c249" }, "outputs": [], "source": [ "class TestClass2(unittest.TestCase):\n", "\n", " @classmethod\n", " def setUpClass(cls):\n", " !touch test_foo.py\n", "\n", " @classmethod\n", " def tearDownClass(cls):\n", " !rm -rf nosedir\n", " !rm -rf test_foo.py\n", "\n", " def test_nosedir_test_directory_created(self):\n", " if IN_COLAB:\n", " !{exec_path} /content/casa6/casatestutils/runtest.py test_foo.py\n", " else:\n", " !{exec_path} {cwd}/work/casa6/casatestutils/runtest.py test_foo.py\n", " self.assertTrue(os.path.exists(\"nosedir/test_foo\"))" ] }, { "cell_type": "markdown", "metadata": { "id": "HgGYK0SeVFT6" }, "source": [ "## Test Case 3\n", "\n", "Test that runtest copies local test to working directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "vGfLBh_hT6eg" }, "outputs": [], "source": [ "class TestClass3(unittest.TestCase):\n", " \n", " @classmethod\n", " def setUpClass(cls):\n", " !touch test_foo.py\n", "\n", " @classmethod\n", " def tearDownClass(cls):\n", " !rm -rf nosedir\n", " !rm -rf test_foo.py\n", "\n", " def test_nosedir_test_copied(self):\n", " if IN_COLAB:\n", " !{exec_path} /content/casa6/casatestutils/runtest.py test_foo.py\n", " else:\n", " !{exec_path} {cwd}/work/casa6/casatestutils/runtest.py test_foo.py\n", " self.assertTrue(os.path.exists(\"nosedir/test_foo/test_foo.py\"))" ] }, { "cell_type": "markdown", "metadata": { "id": "ENXSNIrAZ8Lg" }, "source": [ "## Test Case 4\n", "\n", "Test git cloned test repositories\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "nDnzQ2hXc9VU" }, "outputs": [], "source": [ "class TestClass4(unittest.TestCase):\n", "\n", " @classmethod\n", " def tearDownClass(cls):\n", " !rm -rf nosedir\n", " \n", " def test_nosedir_test_copied(self):\n", " if IN_COLAB:\n", " !{exec_path} /content/casa6/casatestutils/runtest.py -x test_task_tclean\n", " else:\n", " !{exec_path} {cwd}/work/casa6/casatestutils/runtest.py -x test_task_tclean\n", " self.assertTrue(os.path.exists(\"nosedir/casasources/casa6\"))\n", " self.assertTrue(os.path.exists(\"nosedir/casasources/casampi\"))\n", " self.assertTrue(os.path.exists(\"nosedir/casasources/casaplotms\"))\n", " self.assertTrue(os.path.exists(\"nosedir/casasources/casaviewer\"))" ] }, { "cell_type": "markdown", "metadata": { "id": "pHI3_Kh-j6Wr" }, "source": [ "## Test Case 5a" ] }, { "cell_type": "markdown", "metadata": { "id": "CrBVLCkf9w0P" }, "source": [ "Test that runtest runs a casatool test script from git" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "j3l_swxrhI8I", "outputId": "5c2519b1-1827-4d76-8145-8908611a76cb" }, "outputs": [], "source": [ "if IN_COLAB:\n", " !{exec_path} /content/casa6/casatestutils/runtest.py -x test_tool_componentlist\n", "else:\n", " !{exec_path} {cwd}/work/casa6/casatestutils/runtest.py -x test_tool_componentlist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Case 5b - optional" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Test that runtest runs a casatask test script from git" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "KxAEGRMioGVB", "outputId": "8acc9304-8e6b-4400-e3f8-56cee7cb570e" }, "outputs": [], "source": [ "### Run If Need Be\n", "#if IN_COLAB:\n", "# %cd /content/casa6/casatestutils\n", "# !{exec_path} runtest.py test_task_plotweather\n", "# %cd /content/\n", "#else:\n", "# %cd {cwd}/work/casa6/casatestutils\n", "# !{exec_path} runtest.py test_task_plotweather\n", "# %cd {cwd}" ] }, { "cell_type": "markdown", "metadata": { "id": "N7awMTXW_lWf" }, "source": [ "# Test Case 6 \n", "\n", "run a single test case\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "2GvrUYDB_j1A", "outputId": "04ee65b2-fc7e-4baa-df18-83d0e1e08b05" }, "outputs": [], "source": [ "if IN_COLAB:\n", " !{exec_path} /content/casa6/casatestutils/runtest.py test_tool_componentlist[test_plp]\n", "else:\n", " !{exec_path} {cwd}/work/casa6/casatestutils/runtest.py test_tool_componentlist[test_plp]\n" ] }, { "cell_type": "markdown", "metadata": { "id": "ElnPfNLR_wUI" }, "source": [ "# Test Case 7\n", "\n", "run two test cases" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "RX-pTs0J_1VS", "outputId": "aa36e39f-75a5-4917-cb42-676555e3c2de" }, "outputs": [], "source": [ "if IN_COLAB:\n", " %cd /content/casa6/casatestutils\n", " !{exec_path} runtest.py test_tool_componentlist[test_plp,test_summarize,test_getfluxerror]\n", " %cd /content\n", "else:\n", " %cd {cwd}/work/casa6/casatestutils/\n", " !{exec_path} runtest.py test_tool_componentlist[test_plp,test_summarize,test_getfluxerror]\n", " %cd {cwd}" ] }, { "cell_type": "markdown", "metadata": { "id": "j1mRZ_mwhevw" }, "source": [ "# Test Case 8\n", "\n", "run test using --ignore-list" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "OW71Qug4hcJc" }, "outputs": [], "source": [ "class TestClass8(unittest.TestCase):\n", " \n", " @classmethod\n", " def tearDownClass(cls):\n", " !rm -rf nosedir\n", "\n", " def test_ignore_list(self):\n", " if IN_COLAB:\n", " !{exec_path} /content/casa6/casatestutils/runtest.py --ignore_list=test_task_tclean test_tool_componentlist,test_task_tclean\n", " else:\n", " !{exec_path} {cwd}/work/casa6/casatestutils/runtest.py --ignore_list=test_task_tclean test_tool_componentlist,test_task_tclean\n", " self.assertTrue(os.path.exists(\"nosedir/test_tool_componentlist\"))\n", " self.assertFalse(os.path.exists(\"nosedir/test_task_tclean\"))" ] }, { "cell_type": "markdown", "metadata": { "id": "-PTNaDgeIFyA" }, "source": [ "# Test Case 9\n", "Run two test scripts selecting one test case from each one of them" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "EgtkU0pXIUQT", "outputId": "5acf00bd-ca1f-4d87-f30c-bf402dbed771", "scrolled": false }, "outputs": [], "source": [ "if IN_COLAB:\n", " %cd /content/casa6/casatestutils\n", " !{exec_path} runtest.py test_tool_componentlist[test_plp],test_tool_image[test_general]\n", " %cd /content\n", "else:\n", " %cd {cwd}/work/casa6/casatestutils/\n", " !{exec_path} runtest.py test_tool_componentlist[test_plp],test_tool_image[test_general]\n", " %cd {cwd}" ] }, { "cell_type": "markdown", "metadata": { "id": "lrB0lC6SJWoK" }, "source": [ "# Test 10\n", "Run two test script, one with two test cases, the other in full" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "dq3cK8VYJcui", "outputId": "4ad09b89-81bc-48a2-dda9-492b8eb5a530", "scrolled": false }, "outputs": [], "source": [ "if IN_COLAB:\n", " %cd /content/casa6/casatestutils\n", " !{exec_path} runtest.py test_tool_image[test_general,test_stretch],test_tool_componentlist\n", " %cd /content\n", "else:\n", " %cd {cwd}/work/casa6/casatestutils/\n", " !{exec_path} runtest.py test_tool_image[test_general,test_stretch],test_tool_componentlist\n", " %cd {cwd}" ] }, { "cell_type": "markdown", "metadata": { "id": "g_SOYysB_s1u" }, "source": [ "# Test 11" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "ifrqQnRa_og4", "outputId": "eb54778d-fb31-468f-e9a9-7fa08f220e5b" }, "outputs": [], "source": [ "if IN_COLAB:\n", " %cd /content/casa6/casatestutils\n", " !{exec_path} runtest.py test_tool_componentlist,test_tool_image[test_general,test_stretch]\n", " %cd /content\n", "else:\n", " %cd {cwd}/work/casa6/casatestutils/\n", " !{exec_path} runtest.py test_tool_componentlist,test_tool_image[test_general,test_stretch]\n", " %cd {cwd}" ] }, { "cell_type": "markdown", "metadata": { "id": "0Y_l6or2HXyX" }, "source": [ "# Test 12" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "APYU246DHc2J", "outputId": "6c6bfb80-1cc3-442f-ea3f-4bc9ffa4f296", "scrolled": false }, "outputs": [], "source": [ "if IN_COLAB:\n", " %cd /content/casa6/casatestutils\n", " !{exec_path} runtest.py -x -j fixplanets\n", " %cd /content\n", "else:\n", " %cd {cwd}/work/casa6/casatestutils/\n", " !{exec_path} runtest.py -x -j fixplanets\n", " %cd {cwd}" ] }, { "cell_type": "markdown", "metadata": { "id": "OIAFgNqKYud_" }, "source": [ "# Run Tests" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "tXUK9eqeYtw8", "outputId": "b143dcad-55a8-4cc8-b9f8-f108c93e5bef", "scrolled": false }, "outputs": [], "source": [ "res = unittest.main(argv=[''], verbosity=3, exit=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Clean Up" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if not IN_COLAB:\n", " !rm -rf {cwd}/work" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "test_runtest.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 1 }