Merge lp:~james-w/pkgme/better-json-error-message into lp:pkgme

Proposed by James Westby
Status: Merged
Approved by: Jonathan Lange
Approved revision: 82
Merged at revision: 83
Proposed branch: lp:~james-w/pkgme/better-json-error-message
Merge into: lp:pkgme
Diff against target: 50 lines (+23/-1)
2 files modified
pkgme/project_info.py (+7/-1)
pkgme/tests/test_project_info.py (+16/-0)
To merge this branch: bzr merge lp:~james-w/pkgme/better-json-error-message
Reviewer Review Type Date Requested Status
Jonathan Lange Approve
Review via email: mp+80822@code.launchpad.net

Description of the change

Hi,

Another simple fix of the "improve error message" variety.

I don't like the repr much, but not sure what's the best thing to do.

Thanks,

James

To post a comment you must log in.
Revision history for this message
Jonathan Lange (jml) wrote :

Definitely an improvement. Don't know what to do about the repr().

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'pkgme/project_info.py'
2--- pkgme/project_info.py 2011-08-10 14:57:02 +0000
3+++ pkgme/project_info.py 2011-10-31 16:41:28 +0000
4@@ -1,4 +1,5 @@
5 import json
6+import os
7 from pprint import pformat
8
9 from pkgme.run_script import run_script, ScriptMissing
10@@ -61,7 +62,12 @@
11 out = run_script(
12 self.basepath, self.INFO_SCRIPT_NAME, self.cwd,
13 to_write=json.dumps(keys))
14- ret = json.loads(out)
15+ try:
16+ ret = json.loads(out)
17+ except ValueError:
18+ raise AssertionError("%s didn't return valid JSON: %r"
19+ % (os.path.join(self.basepath, self.INFO_SCRIPT_NAME),
20+ out))
21 trace.debug('%s returned %s' % (self.INFO_SCRIPT_NAME, pformat(ret)))
22 return ret
23
24
25=== modified file 'pkgme/tests/test_project_info.py'
26--- pkgme/tests/test_project_info.py 2011-08-10 14:53:01 +0000
27+++ pkgme/tests/test_project_info.py 2011-10-31 16:41:28 +0000
28@@ -131,6 +131,22 @@
29 info = SingleExternalHelperInfo(tempdir.path, tempdir.path)
30 self.assertEqual({script_name: value}, info.get_all([script_name]))
31
32+ def test_when_script_gives_invalid_json_output(self):
33+ """Test when the script doesn't produce valid JSON.
34+
35+ As this is a problem with the backend we expect an AssertionError
36+ with an intelligible message.
37+ """
38+ tempdir = self.useFixture(TempdirFixture())
39+ script_path = tempdir.abspath(SingleExternalHelperInfo.INFO_SCRIPT_NAME)
40+ value = self.getUniqueString()
41+ script = '#!%s\nprint "}Nonsense"' % (sys.executable, )
42+ self.useFixture(ExecutableFileFixture(script_path, script))
43+ info = SingleExternalHelperInfo(tempdir.path, tempdir.path)
44+ e = self.assertRaises(AssertionError, info.get_all, [])
45+ self.assertEquals("%s didn't return valid JSON: '}Nonsense\\n'"
46+ % (script_path, ), str(e))
47+
48 def test_passes_input(self):
49 tempdir = self.useFixture(TempdirFixture())
50 script_name = self.getUniqueString()

Subscribers

People subscribed via source and target branches

to all changes: