[augeas-devel] augeas: master - Record new files under /augeas/files when they are saved

David Lutterkort lutter at fedoraproject.org
Tue Aug 11 00:58:38 UTC 2009


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=2b43c30d057a0483b039b4159e5be6e97dff0e16
Commit:        2b43c30d057a0483b039b4159e5be6e97dff0e16
Parent:        38281e4be29e02fb109a32723cc7a7d102560ff9
Author:        David Lutterkort <lutter at redhat.com>
AuthorDate:    Thu Aug 6 19:14:13 2009 -0700
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Thu Aug 6 19:16:19 2009 -0700

Record new files under /augeas/files when they are saved

  * src/transform.c (transform_save): add file info with add_file_info
  * tests/test-save.c: test that file info is added for a new file
  * tests/Makefile.am: build and run test-save
  * .gitignore: ignore tests/test-save

Fixes bug #78
---
 .gitignore        |    1 +
 src/transform.c   |    5 ++
 tests/Makefile.am |    5 ++-
 tests/test-save.c |  141 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 151 insertions(+), 1 deletions(-)

diff --git a/.gitignore b/.gitignore
index d881658..a13ebd9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,6 +37,7 @@ tests/fatest
 tests/lens-*.sh
 tests/test-xpath
 tests/test-load
+tests/test-save
 *.aux
 *.dvi
 *.log
diff --git a/src/transform.c b/src/transform.c
index 4d382b2..f0b4775 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -832,6 +832,11 @@ int transform_save(struct augeas *aug, struct tree *xfm,
     result = 1;
 
  done:
+    r = add_file_info(aug, path, lens);
+    if (r < 0) {
+        err_status = "file_info";
+        result = -1;
+    }
     if (result > 0) {
         r = file_saved_event(aug, path);
         if (r < 0) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8c13bcf..a44d0eb 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -104,7 +104,7 @@ EXTRA_DIST = \
 
 noinst_SCRIPTS = $(check_SCRIPTS)
 
-check_PROGRAMS = fatest test-xpath test-load
+check_PROGRAMS = fatest test-xpath test-load test-save
 
 TESTS_ENVIRONMENT = \
   PATH='$(abs_top_builddir)/src$(PATH_SEPARATOR)'"$$PATH" \
@@ -123,3 +123,6 @@ test_xpath_LDADD = $(top_builddir)/src/libaugeas.la $(GNULIB)
 
 test_load_SOURCES = test-load.c cutest.c cutest.h
 test_load_LDADD = $(top_builddir)/src/libaugeas.la $(GNULIB)
+
+test_save_SOURCES = test-save.c cutest.c cutest.h
+test_save_LDADD = $(top_builddir)/src/libaugeas.la $(GNULIB)
diff --git a/tests/test-save.c b/tests/test-save.c
new file mode 100644
index 0000000..a1a9052
--- /dev/null
+++ b/tests/test-save.c
@@ -0,0 +1,141 @@
+/*
+ * test-save.c: test various aspects of saving
+ *
+ * Copyright (C) 2009 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ * Author: David Lutterkort <lutter at redhat.com>
+ */
+
+#include <config.h>
+#include "augeas.h"
+#include "internal.h"
+#include "cutest.h"
+
+#include <stdio.h>
+
+const char *abs_top_srcdir;
+const char *abs_top_builddir;
+char *root = NULL, *src_root = NULL;
+struct augeas *aug = NULL;
+
+#define die(msg)                                                    \
+    do {                                                            \
+        fprintf(stderr, "%s:%d: Fatal error: %s\n", __FILE__, __LINE__, msg); \
+        exit(EXIT_FAILURE);                                         \
+    } while(0)
+
+static void run(CuTest *tc, const char *format, ...) {
+    char *command;
+    va_list args;
+    int r;
+
+    va_start(args, format);
+    r = vasprintf(&command, format, args);
+    va_end (args);
+    if (r < 0)
+        CuFail(tc, "Failed to format command (out of memory)");
+    r = system(command);
+    if (r < 0 || (WIFEXITED(r) && WEXITSTATUS(r) != 0)) {
+        char *msg;
+        r = asprintf(&msg, "Command %s failed with status %d\n",
+                     command, WEXITSTATUS(r));
+        CuFail(tc, msg);
+        free(msg);
+    }
+}
+
+static void setup(CuTest *tc) {
+    char *lensdir;
+
+    if (asprintf(&root, "%s/build/test-save/%s",
+                 abs_top_builddir, tc->name) < 0) {
+        CuFail(tc, "failed to set root");
+    }
+
+    if (asprintf(&lensdir, "%s/lenses", abs_top_srcdir) < 0)
+        CuFail(tc, "asprintf lensdir failed");
+
+    run(tc, "test -d %s && chmod -R u+w %s || :", root, root);
+    run(tc, "rm -rf %s", root);
+    run(tc, "mkdir -p %s", root);
+    run(tc, "cp -pr %s/* %s", src_root, root);
+    run(tc, "chmod -R u+w %s", root);
+
+    aug = aug_init(root, lensdir, AUG_NO_STDINC);
+    CuAssertPtrNotNull(tc, aug);
+}
+
+static void teardown(ATTRIBUTE_UNUSED CuTest *tc) {
+    aug_close(aug);
+    aug = NULL;
+    free(root);
+    root = NULL;
+}
+
+static void testSaveNewFile(CuTest *tc) {
+    int r;
+
+    r = aug_match(aug, "/augeas/files/etc/yum.repos.d/new.repo/path", NULL);
+    CuAssertIntEquals(tc, 0, r);
+
+    r = aug_set(aug, "/files/etc/yum.repos.d/new.repo/newrepo/baseurl",
+                "http://foo.com/");
+    CuAssertIntEquals(tc, 0, r);
+
+    r = aug_save(aug);
+    CuAssertIntEquals(tc, 0, r);
+
+    r = aug_match(aug, "/augeas/files/etc/yum.repos.d/new.repo/path", NULL);
+    CuAssertIntEquals(tc, 1, r);
+}
+
+int main(void) {
+    char *output = NULL;
+    CuSuite* suite = CuSuiteNew();
+
+    abs_top_srcdir = getenv("abs_top_srcdir");
+    if (abs_top_srcdir == NULL)
+        die("env var abs_top_srcdir must be set");
+
+    abs_top_builddir = getenv("abs_top_builddir");
+    if (abs_top_builddir == NULL)
+        die("env var abs_top_builddir must be set");
+
+    if (asprintf(&src_root, "%s/tests/root", abs_top_srcdir) < 0) {
+        die("failed to set src_root");
+    }
+
+    CuSuiteSetup(suite, setup, teardown);
+
+    SUITE_ADD_TEST(suite, testSaveNewFile);
+
+    CuSuiteRun(suite);
+    CuSuiteSummary(suite, &output);
+    CuSuiteDetails(suite, &output);
+    printf("%s\n", output);
+    free(output);
+    return suite->failCount;
+}
+
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 4
+ * End:
+ */




More information about the augeas-devel mailing list