diff options
author | Enrico Zini <enrico@enricozini.org> | 2015-06-17 22:33:06 +0200 |
---|---|---|
committer | Enrico Zini <enrico@enricozini.org> | 2015-06-17 22:33:06 +0200 |
commit | f3fd2b05c7c620edd0aae8330bf43c5246459300 (patch) | |
tree | 5d9764431a4d3e9a6d4156a140f5b326b82228ea | |
parent | 34753f3d4d2b05eb8b97bd51c6920c1850d51ecb (diff) | |
download | libept-f3fd2b05c7c620edd0aae8330bf43c5246459300.tar.gz |
Decouple Vocabulary from maint/ directory management
-rw-r--r-- | ept/CMakeLists.txt | 4 | ||||
-rw-r--r-- | ept/debtags/vocabulary.cc | 59 | ||||
-rw-r--r-- | ept/debtags/vocabulary.h | 11 | ||||
-rw-r--r-- | ept/debtags/vocabulary.test.h | 144 | ||||
-rw-r--r-- | ept/test.h | 28 |
5 files changed, 136 insertions, 110 deletions
diff --git a/ept/CMakeLists.txt b/ept/CMakeLists.txt index a74bf8e..96e1746 100644 --- a/ept/CMakeLists.txt +++ b/ept/CMakeLists.txt @@ -64,8 +64,8 @@ add_custom_command( COMMAND cp -a ${datadir}/etc/sources.list test-env/etc/ COMMAND sed -e s,i386,${ARCH}, < ${datadir}/dpkg-status > test-env/dpkg-status COMMAND cp -a ${datadir}/desktop/*.desktop test-env/desktop/ - COMMAND cp ${datadir}/debtags/package-tags test-env/debtags/test.tag - COMMAND cp ${datadir}/debtags/vocabulary test-env/debtags/test.voc + COMMAND cp ${datadir}/debtags/package-tags test-env/debtags/package-tags + COMMAND cp ${datadir}/debtags/vocabulary test-env/debtags/vocabulary COMMAND mkdir -p test-env/debtags/empty COMMAND mkdir -p test-env/debtags/user COMMAND mkdir -p test-env/xapian/ diff --git a/ept/debtags/vocabulary.cc b/ept/debtags/vocabulary.cc index 5c84ea4..504fab2 100644 --- a/ept/debtags/vocabulary.cc +++ b/ept/debtags/vocabulary.cc @@ -20,20 +20,19 @@ #include <ept/debtags/vocabulary.h> #include <ept/debtags/maint/debdbparser.h> -#include <ept/debtags/maint/path.h> -#include <ept/debtags/maint/sourcedir.h> - #include <tagcoll/input/memory.h> - +#include <tagcoll/input/stdio.h> +#include <wibble/sys/fs.h> #include <cstring> #include <cstdio> +#include <cstdlib> #include <sstream> - #include <sys/types.h> #include <unistd.h> using namespace std; using namespace tagcoll; +using namespace wibble; namespace ept { namespace debtags { @@ -95,23 +94,30 @@ std::set<std::string> FacetData::tags() const } Vocabulary::Vocabulary(bool empty) + : m_timestamp(0) { - if (!empty) - { - SourceDir mainSource(Path::debtagsSourceDir()); - SourceDir userSource(Path::debtagsUserSourceDir()); + if (empty) return; + load(pathname()); +} - mainSource.readVocabularies(*this); - userSource.readVocabularies(*this); +Vocabulary::~Vocabulary() +{ +} - time_t ts_main_src = mainSource.vocTimestamp(); - time_t ts_user_src = userSource.vocTimestamp(); - m_timestamp = ts_main_src > ts_user_src ? ts_main_src : ts_user_src; - } +string Vocabulary::pathname() +{ + const char* res = getenv("DEBTAGS_VOCABULARY"); + if (!res) res = "/var/lib/debtags/vocabulary"; + return res; } -Vocabulary::~Vocabulary() +void Vocabulary::load(const std::string& pathname) { + if (!sys::fs::exists(pathname)) return; + // Read uncompressed data + tagcoll::input::Stdio in(pathname); + read(in); + m_timestamp = sys::fs::timestamp(pathname, 0); } voc::TagData& voc::FacetData::obtainTag(const std::string& name) @@ -247,23 +253,12 @@ void Vocabulary::read(tagcoll::input::Input& input) void Vocabulary::write() { - SourceDir mainSource(Path::debtagsSourceDir()); - SourceDir userSource(Path::debtagsUserSourceDir()); - - // Do we have a user source? - time_t ts_user_src = userSource.vocTimestamp(); - - // Find out what vocabulary we should write - string vocfname; - if (ts_user_src > 0) - vocfname = Path::userVocabulary(); - else - vocfname = Path::vocabulary(); + string vocfname = pathname(); - // Write out, with appropriate umask - mode_t prev_umask = umask(022); - write(vocfname); - umask(prev_umask); + // Write out, with appropriate umask + mode_t prev_umask = umask(022); + write(vocfname); + umask(prev_umask); } void Vocabulary::write(const std::string& fname) diff --git a/ept/debtags/vocabulary.h b/ept/debtags/vocabulary.h index 7d0bc7e..a5a5fa3 100644 --- a/ept/debtags/vocabulary.h +++ b/ept/debtags/vocabulary.h @@ -226,6 +226,9 @@ public: FacetSet facets(const FacetMatcher& filter) const throw () { return getFiltered(filter); } #endif + /// Load vocabulary data from the given file + void load(const std::string& pathname); + /** * Parse and import the vocabulary from `input', merging the data with the * previously imported ones @@ -246,6 +249,14 @@ public: * Write the vocabulary data to the given output stream */ void write(FILE* out); + + /** + * Return the default pathname for the vocabulary. + * + * This returns /var/lib/debtags/vocabulary, unless it has been overridden + * by setting $DEBTAGS_VOCABULARY + */ + static std::string pathname(); }; } diff --git a/ept/debtags/vocabulary.test.h b/ept/debtags/vocabulary.test.h index a6ed1b8..0635d6f 100644 --- a/ept/debtags/vocabulary.test.h +++ b/ept/debtags/vocabulary.test.h @@ -23,42 +23,48 @@ #include <ept/debtags/maint/path.h> #include <tagcoll/utils/set.h> #include <tagcoll/input/stdio.h> - #include "ept/test.h" using namespace std; using namespace tagcoll::utils; using namespace ept::debtags; +#define testfile TEST_ENV_DIR "debtags/vocabulary" + + struct TestVocabulary : DebtagsTestEnvironment { - Vocabulary m_tags; - Vocabulary& tags() { return m_tags; } - Test _1() { - tags(); // this will throw if the open above didn't work + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; // this will throw if it failed to load } Test _2() { - assert( tags().hasFacet( "works-with" ) ); - assert( !tags().hasFacet( "blah" ) ); + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; + assert( tags.hasFacet( "works-with" ) ); + assert( !tags.hasFacet( "blah" ) ); } Test _3() { - assert( tags().hasTag( "works-with::people" ) ); - assert( !tags().hasTag( "works-with::midgets" ) ); + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; + assert( tags.hasTag( "works-with::people" ) ); + assert( !tags.hasTag( "works-with::midgets" ) ); } Test _4() { - const voc::TagData *people = tags().tagData( "works-with::people" ), - *midgets = tags().tagData( "works-with::midgets" ), - *blahg = tags().tagData( "works-with::blahg" ), - *text = tags().tagData( "works-with::text" ), - *people2 = tags().tagData( "works-with::people" ); + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; + const voc::TagData *people = tags.tagData( "works-with::people" ), + *midgets = tags.tagData( "works-with::midgets" ), + *blahg = tags.tagData( "works-with::blahg" ), + *text = tags.tagData( "works-with::text" ), + *people2 = tags.tagData( "works-with::people" ); assert( people != midgets ); assert( people != text ); assert( people != blahg ); @@ -70,11 +76,13 @@ struct TestVocabulary : DebtagsTestEnvironment Test _5() { + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; std::string a = "works-with::people", b = "works-with::midgets"; - std::set<std::string> s = tags().tags(), - f = tags().tags( "works-with" ), - n = tags().tags( "nonsense" ); + std::set<std::string> s = tags.tags(), + f = tags.tags( "works-with" ), + n = tags.tags( "nonsense" ); assert( set_contains(s, a) ); assert( set_contains(f, a) ); assert( set_contains(s, f) ); @@ -85,31 +93,43 @@ struct TestVocabulary : DebtagsTestEnvironment Test _6() { - const voc::FacetData* f = tags().facetData( "works-with" ); + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; + + const voc::FacetData* f = tags.facetData( "works-with" ); assert(f); assert_eq(f->name, "works-with"); - const voc::TagData* t = tags().tagData( "works-with::people" ); + const voc::TagData* t = tags.tagData( "works-with::people" ); assert(t); assert_eq(t->name, "works-with::people"); } Test _7() { - const voc::FacetData* f = tags().facetData( "works-with" ); - std::set<std::string> x = tags().tags( "works-with" ); + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; + + const voc::FacetData* f = tags.facetData( "works-with" ); + std::set<std::string> x = tags.tags( "works-with" ); assert( x == f->tags() ); } Test _8() { - const voc::FacetData* f = tags().facetData( "does-not-work-with" ); + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; + + const voc::FacetData* f = tags.facetData( "does-not-work-with" ); assert(!f); } Test _9() { - const voc::FacetData* f = tags().facetData( "legacy" ); + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; + + const voc::FacetData* f = tags.facetData( "legacy" ); assert(f); assert_eq(f->shortDescription(), ""); assert_eq(f->longDescription(), ""); @@ -118,31 +138,40 @@ struct TestVocabulary : DebtagsTestEnvironment Test _10() { + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; + // assert that one-character tag names are parsed correctly - assert( tags().hasTag( "implemented-in::c" ) ); + assert( tags.hasTag( "implemented-in::c" ) ); } Test _11() { + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; + // assert that all facets are somehow working - std::set<std::string> facets = tags().facets(); + std::set<std::string> facets = tags.facets(); for (std::set<std::string>::const_iterator i = facets.begin(); i != facets.end(); i++) { - const voc::FacetData* f = tags().facetData(*i); + const voc::FacetData* f = tags.facetData(*i); assert(f); } } Test _12() { + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary voc; + // assert that all tags are somehow working - std::set<std::string> tags = this->tags().tags(); + std::set<std::string> tags = voc.tags(); for (std::set<std::string>::const_iterator i = tags.begin(); i != tags.end(); i++) { - const voc::TagData* t = this->tags().tagData(*i); + const voc::TagData* t = voc.tagData(*i); assert(t); } } @@ -150,7 +179,8 @@ struct TestVocabulary : DebtagsTestEnvironment // Check for correctness of the first and last tag in the vocabulary Test _13() { - Vocabulary& tags = this->tags(); + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; const voc::TagData* first = tags.tagData("accessibility::TODO"); assert(first); @@ -165,58 +195,22 @@ struct TestVocabulary : DebtagsTestEnvironment Test _14() { - // assert that it's possible to go from facet to ID and back - // we don't use IDs anymore -} + EnvOverride eo("DEBTAGS_VOCABULARY", testfile); + Vocabulary tags; - Test _15() -{ - // assert that it's possible to go from tag to ID and back - // we don't use IDs anymore -} + // check that we're seeing all the tags for a facet + std::set<std::string> t = tags.tags("accessibility"); + assert_eq(t.size(), 10u); - Test _16() -{ - // assert that facet IDs are distinct - // we don't use IDs anymore -} - - Test _17() -{ - // assert that tag IDs are distinct - // we don't use IDs anymore -} - - Test _18() -{ - // assert that all the tags are indexed - // we don't use the index anymore -} - - Test _19() -{ - // test the tagcmp function - // we don't have tagcmp anymore -} - - Test _20() -{ - // check that we're seeing all the tags for a facet - std::set<std::string> t = tags().tags("accessibility"); - assert_eq(t.size(), 10u); - - t = tags().tags("works-with-format"); + t = tags.tags("works-with-format"); assert_eq(t.size(), 33u); } // If there is no data, Vocabulary should work as an empty vocabulary - Test _21() + Test _15() { - Path::OverrideDebtagsSourceDir odsd("./empty"); - Path::OverrideDebtagsIndexDir odid("./empty"); - Path::OverrideDebtagsUserSourceDir odusd("./empty"); - Path::OverrideDebtagsUserIndexDir oduid("./empty"); - Vocabulary empty; + EnvOverride eo("DEBTAGS_VOCABULARY", "./empty/novocabularyhere"); + Vocabulary empty; assert(!empty.hasData()); @@ -228,5 +222,3 @@ struct TestVocabulary : DebtagsTestEnvironment } }; - -// vim:set ts=4 sw=4: @@ -12,6 +12,7 @@ #include <apt-pkg/progress.h> #include <apt-pkg/pkgcachegen.h> #include <apt-pkg/init.h> +#include <cstdlib> #ifndef EPT_TEST_H @@ -46,4 +47,31 @@ struct DebtagsTestEnvironment : AptTestEnvironment { {} }; +struct EnvOverride +{ + const char* name; + bool old_value_set; + std::string old_value; + + EnvOverride(const char* name, const char* value) + : name(name) + { + const char* old = getenv(name); + if (old) + { + old_value_set = true; + old_value = old; + } else + old_value_set = false; + setenv(name, value, 1); + } + ~EnvOverride() + { + if (old_value_set) + setenv(name, old_value.c_str(), 1); + else + unsetenv(name); + } +}; + #endif |