Merge lp:~mvo/click/dont-crash-for-empty-db into lp:click/devel

Proposed by Michael Vogt
Status: Merged
Approved by: Michael Vogt
Approved revision: 479
Merged at revision: 560
Proposed branch: lp:~mvo/click/dont-crash-for-empty-db
Merge into: lp:click/devel
Diff against target: 102 lines (+41/-3)
2 files modified
click/tests/test_database.py (+12/-0)
lib/click/database.vala (+29/-3)
To merge this branch: bzr merge lp:~mvo/click/dont-crash-for-empty-db
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
click hackers Pending
Review via email: mp+224318@code.launchpad.net

Commit message

Do not crash when no database configuration is used and Click.DB.{get,props.overlay,gc,ensure_ownership} are called.

Description of the change

This branch adds checks into libclick to avoid crashing if no database configuration is loaded.

This is a better version of the (rightfully) reverted r474. I currently have a single error type for this. If you think the case "no database at all" and "invalid index in get()" should be seperate error types I'm happy to do that.

AIUI this is a ABI break as get() now throws a error when it wasn't before (i.e. a GError is now part of the get function signature). So we may need to defer this until the next abi break or return "null" or something instead of raising a error.

Thanks,
 Michael

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :

FAILED: Continuous integration, rev:478
No commit message was specified in the merge proposal. Click on the following link and set the commit message (if you want a jenkins rebuild you need to trigger it yourself):
https://code.launchpad.net/~mvo/click/dont-crash-for-empty-db/+merge/224318/+edit-commit-message

http://jenkins.qa.ubuntu.com/job/click-devel-ci/4/
Executed test runs:
    SUCCESS: http://jenkins.qa.ubuntu.com/job/click-devel-utopic-amd64-ci/4
    SUCCESS: http://jenkins.qa.ubuntu.com/job/click-devel-utopic-armhf-ci/4
        deb: http://jenkins.qa.ubuntu.com/job/click-devel-utopic-armhf-ci/4/artifact/work/output/*zip*/output.zip
    SUCCESS: http://jenkins.qa.ubuntu.com/job/click-devel-utopic-i386-ci/4

Click here to trigger a rebuild:
http://s-jenkins.ubuntu-ci:8080/job/click-devel-ci/4/rebuild

review: Needs Fixing (continuous-integration)
479. By Michael Vogt

merged lp:click/devel

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Barry Warsaw (barry) wrote :

>This is a better version of the (rightfully) reverted r474. I currently have a single error >type for this. If you think the case "no database at all" and "invalid index in get()" should >be seperate error types I'm happy to do that.

Would a typical user be able to do something different in these two cases? If so then having separate errors makes sense. If not, then it probably doesn't effectively matter (except perhaps for API consistency, or future proofing, but OTOH YAGNI?).

Revision history for this message
Michael Vogt (mvo) wrote :

Thanks for the review! I'm in favor of YAGNI in this case :)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'click/tests/test_database.py'
2--- click/tests/test_database.py 2014-09-10 11:54:14 +0000
3+++ click/tests/test_database.py 2014-09-29 11:41:01 +0000
4@@ -562,6 +562,18 @@
5 db = Click.DB()
6 self.assertEqual(0, db.props.size)
7
8+ def test_no_db_conf_errors(self):
9+ db = Click.DB()
10+ self.assertRaisesDatabaseError(
11+ Click.DatabaseError.INVALID, db.get, 0)
12+ self.assertEqual(db.props.overlay, "")
13+ self.assertRaisesDatabaseError(
14+ Click.DatabaseError.INVALID, db.maybe_remove, "something", "1.0")
15+ self.assertRaisesDatabaseError(
16+ Click.DatabaseError.INVALID, db.gc)
17+ self.assertRaisesDatabaseError(
18+ Click.DatabaseError.INVALID, db.ensure_ownership)
19+
20 def test_read_nonexistent(self):
21 db = Click.DB()
22 db.read(db_dir=os.path.join(self.temp_dir, "nonexistent"))
23
24=== modified file 'lib/click/database.vala'
25--- lib/click/database.vala 2014-09-12 11:46:05 +0000
26+++ lib/click/database.vala 2014-09-29 11:41:01 +0000
27@@ -34,7 +34,11 @@
28 /**
29 * Package manifest cannot be parsed.
30 */
31- BAD_MANIFEST
32+ BAD_MANIFEST,
33+ /**
34+ * No database available for the given request
35+ */
36+ INVALID
37 }
38
39 private string? app_pid_command = null;
40@@ -602,8 +606,11 @@
41 public int size { get { return db.size; } }
42
43 public new SingleDB
44- @get (int index)
45+ @get (int index) throws DatabaseError
46 {
47+ if (index >= db.size)
48+ throw new DatabaseError.INVALID
49+ ("invalid index %i for db of size %i", index, db.size);
50 return db.get (index);
51 }
52
53@@ -618,7 +625,15 @@
54 *
55 * The directory where changes should be written.
56 */
57- public string overlay { get { return db.last ().root; } }
58+ public string overlay
59+ {
60+ get {
61+ if (db.size == 0)
62+ return "";
63+ else
64+ return db.last ().root;
65+ }
66+ }
67
68 /**
69 * get_path:
70@@ -812,21 +827,32 @@
71 return generator.to_data (null);
72 }
73
74+ private void
75+ ensure_db () throws Error
76+ {
77+ if (db.size == 0)
78+ throw new DatabaseError.INVALID
79+ ("no database loaded");
80+ }
81+
82 public void
83 maybe_remove (string package, string version) throws Error
84 {
85+ ensure_db();
86 db.last ().maybe_remove (package, version);
87 }
88
89 public void
90 gc () throws Error
91 {
92+ ensure_db();
93 db.last ().gc ();
94 }
95
96 public void
97 ensure_ownership () throws Error
98 {
99+ ensure_db();
100 db.last ().ensure_ownership ();
101 }
102 }

Subscribers

People subscribed via source and target branches

to all changes: