[augeas-devel] [PATCH 1 of 2] Add support for 'insert' in unit tests

David Lutterkort dlutter at redhat.com
Thu Jul 17 04:06:31 UTC 2008


4 files changed, 70 insertions(+), 5 deletions(-)
src/augeas.c                    |   15 +++++++++-----
src/builtin.c                   |   40 +++++++++++++++++++++++++++++++++++++++
src/internal.h                  |    2 +
tests/modules/pass_ins_test.aug |   18 +++++++++++++++++


# HG changeset patch
# User David Lutterkort <dlutter at redhat.com>
# Date 1216247320 25200
# Node ID a090ab78da11d32de54d1572f8ab115c3120854e
# Parent  6a27483c0c44486e565220c4b8a9ceef1d71cc00
Add support for 'insert' in unit tests

Two new primitives 'insa' (insert after) and 'insb' (insert before) are
added so that unit tests can modify the tree through insert, as in

  test lns put str after
    insa label path = str2

It would be nice to kill insert with one primitive, but that would have
either required special syntax (like 'ins STR before/after STR') or some
addition to the type system to make it syntactically reasonable.

diff -r 6a27483c0c44 -r a090ab78da11 src/augeas.c
--- a/src/augeas.c	Wed Jul 09 10:37:09 2008 -0700
+++ b/src/augeas.c	Wed Jul 16 15:28:40 2008 -0700
@@ -606,15 +606,15 @@
     return tree_set(aug->tree, path, value) == NULL ? -1 : 0;
 }
 
-int aug_insert(struct augeas *aug, const char *path, const char *label,
-               int before) {
+int tree_insert(struct tree **tree, const char *path, const char *label,
+                int before) {
     struct path *p = NULL;
     struct tree *new = NULL;
 
     if (strchr(label, SEP) != NULL)
         return -1;
 
-    p = make_path(aug->tree, path);
+    p = make_path(*tree, path);
     if (p == NULL)
         goto error;
 
@@ -628,8 +628,8 @@
     struct segment *seg = last_segment(p);
     if (before) {
         struct tree *siblings = seg_siblings(p, seg);
-        if (siblings == aug->tree) {
-            list_insert_before(new, seg->tree, aug->tree);
+        if (siblings == *tree) {
+            list_insert_before(new, seg->tree, *tree);
         } else {
             list_insert_before(new, seg->tree, siblings);
         }
@@ -643,6 +643,11 @@
     free_tree(new);
     free_path(p);
     return -1;
+}
+
+int aug_insert(struct augeas *aug, const char *path, const char *label,
+               int before) {
+    return tree_insert(&(aug->tree), path, label, before);
 }
 
 struct tree *make_tree(char *label, char *value, struct tree *children) {
diff -r 6a27483c0c44 -r a090ab78da11 src/builtin.c
--- a/src/builtin.c	Wed Jul 09 10:37:09 2008 -0700
+++ b/src/builtin.c	Wed Jul 16 15:28:40 2008 -0700
@@ -189,6 +189,42 @@
     return ref(tree);
 }
 
+static struct value *tree_insert_glue(struct info *info, struct value *label,
+                                      struct value *path, struct value *tree,
+                                      int before) {
+    // FIXME: This only works if TREE is not referenced more than once;
+    // otherwise we'll have some pretty weird semantics, and would really
+    // need to copy TREE first
+    assert(label->tag == V_STRING);
+    assert(path->tag == V_STRING);
+    assert(tree->tag == V_TREE);
+
+    int r;
+    r = tree_insert(&(tree->tree), path->string->str,
+                    label->string->str, before);
+    if (r != 0) {
+        return make_exn_value(ref(info),
+                              "Tree insert of %s at %s failed",
+                              label->string->str, path->string->str);
+    }
+
+    return ref(tree);
+}
+
+/* Insert after */
+/* V_STRING -> V_STRING -> V_TREE -> V_TREE */
+static struct value *tree_insa_glue(struct info *info, struct value *label,
+                                    struct value *path, struct value *tree) {
+    return tree_insert_glue(info, label, path, tree, 0);
+}
+
+/* Insert before */
+/* V_STRING -> V_STRING -> V_TREE -> V_TREE */
+static struct value *tree_insb_glue(struct info *info, struct value *label,
+                                    struct value *path, struct value *tree) {
+    return tree_insert_glue(info, label, path, tree, 1);
+}
+
 /* V_STRING -> V_TREE -> V_TREE */
 static struct value *tree_rm_glue(struct info *info,
                                   struct value *path,
@@ -270,6 +306,10 @@
     define_native(modl, "set", 3, tree_set_glue, T_STRING, T_STRING, T_TREE,
                                                  T_TREE);
     define_native(modl, "rm", 2, tree_rm_glue, T_STRING, T_TREE, T_TREE);
+    define_native(modl, "insa", 3, tree_insa_glue, T_STRING, T_STRING, T_TREE,
+                                                   T_TREE);
+    define_native(modl, "insb", 3, tree_insb_glue, T_STRING, T_STRING, T_TREE,
+                                                   T_TREE);
     /* Transforms and filters */
     define_native(modl, "incl", 1, xform_incl, T_STRING, T_FILTER);
     define_native(modl, "excl", 1, xform_excl, T_STRING, T_FILTER);
diff -r 6a27483c0c44 -r a090ab78da11 src/internal.h
--- a/src/internal.h	Wed Jul 09 10:37:09 2008 -0700
+++ b/src/internal.h	Wed Jul 16 15:28:40 2008 -0700
@@ -249,6 +249,8 @@
 
 int tree_rm(struct tree **tree, const char *path);
 struct tree *tree_set(struct tree *tree, const char *path, const char *value);
+int tree_insert(struct tree **tree, const char *path, const char *label,
+                int before);
 int free_tree(struct tree *tree);
 int print_tree(const struct tree *tree, FILE *out, const char *path,
                int pr_hidden);
diff -r 6a27483c0c44 -r a090ab78da11 tests/modules/pass_ins_test.aug
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/modules/pass_ins_test.aug	Wed Jul 16 15:28:40 2008 -0700
@@ -0,0 +1,18 @@
+module Pass_ins_test =
+
+  let eol = del "\n" "\n"
+  let word = /[a-z0-9]+/
+  let lns = [ key word . del /[ \t]+/ " " . store word . eol ]*
+
+  let s = "key value1\nkey value2\n"
+  let t = "key value1\nnewkey newvalue\nkey value2\n"
+
+  test lns put s after
+    insa "newkey" "key[1]";
+    set "newkey" "newvalue"
+  = t
+
+  test lns put s after
+    insb "newkey" "key[2]";
+    set "newkey" "newvalue"
+  = t




More information about the augeas-devel mailing list