Repository: newville/pyshortcuts

Issue:
AttributeError crash if shortcut cmd is undefined
If one tries to create a shortcut with an empty cmd it fails with the message `AttributeError: 'NoneType' object has no attribute 'split'`. 

    cmd = shutil.which("my-script")    # not in PATH, so returns nothing
    scut = make_shortcut(cmd, name="My Script", icon=iconpath)

It could be more explicit about what's wrong. I'm not sure where's best place to put an error message, or very clear on the best practice for how.


Discussion:

Patch:
diff --git a/pyshortcuts/shortcut.py b/pyshortcuts/shortcut.py
index 1c155cf..9429df0 100644
--- a/pyshortcuts/shortcut.py
+++ b/pyshortcuts/shortcut.py
@@ -62,6 +62,9 @@ def shortcut(script, userfolders, name=None, description=None, folder=None,
     """
     from . import platform, scut_ext, ico_ext
 
+    if not isinstance(script, str) or len(script) < 1:
+        raise ValueError("`script` for shortcut must be a non-zero length string")
+
     words = script.split(' ', 1)
     if len(words) < 2:
         words.append('')


Test patch:
diff --git a/tests/test_pyshortcuts.py b/tests/test_pyshortcuts.py
index abd9826..2749bb5 100644
--- a/tests/test_pyshortcuts.py
+++ b/tests/test_pyshortcuts.py
@@ -1,13 +1,21 @@
 #!/usr/bin/env python
+'''Normal test run:
+     python -m pytests
+
+   Run and see manual debug print()s:
+     python test_pyshortcuts.py
+'''
 import os
+import pytest
 from pyshortcuts import make_shortcut, platform, Shortcut, get_folders
 
-root = os.path.abspath(os.path.join(__file__, '..', '..'))
-
 folders = get_folders()
+root = os.path.abspath(os.path.join(__file__, '..', '..'))
 
 def test_shortcut():
-    print("---folders:\n Home: {}\n Desktop: {}\n StartMenu: {}\n".format(
+    '''Create a shortcut from examples dir'''
+    print('---test_shortcut()')
+    print("folders:\n Home: {}\n Desktop: {}\n StartMenu: {}\n".format(
         folders.home, folders.desktop, folders.startmenu))
 
     script = os.path.join(root, 'examples', 'console_scripts', 'timer.py') + ' -u 0.25 -t 10'
@@ -21,7 +29,23 @@ def test_shortcut():
     scut = make_shortcut(script, name='Timer', icon=icon, folder=folders.desktop)
     assert isinstance(scut, Shortcut), 'it returns a shortcut instance'
 
-    #raise err # force crash so we see manual print() debug statements
+def test_none_script():
+    '''Verify we get ValueError if cmd is None (#19)'''
+    print('---test_none_script()')
+    with pytest.raises(ValueError):
+        cmd = None
+        scut = make_shortcut(cmd, name="My Script is None")
+    print('OK')
+
+def test_empty_string_script():
+    '''Verify we get ValueError if cmd is an empty string (#19)'''
+    print('---test_empty_script()')
+    with pytest.raises(ValueError):
+        cmd = ""
+        scut = make_shortcut(cmd, name="My Script is empty string")
+    print('OK')
 
 if __name__ == '__main__':
     test_shortcut()
+    test_none_script()
+    test_empty_string_script()