1 | // $Id: copyright_test.cc 693 2008-09-11 20:42:56Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2008 Peter Johansson |
---|
5 | |
---|
6 | This file is part of svndigest, http://dev.thep.lu.se/svndigest |
---|
7 | |
---|
8 | svndigest is free software; you can redistribute it and/or modify it |
---|
9 | under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 3 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | svndigest is distributed in the hope that it will be useful, but |
---|
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include "Suite.h" |
---|
23 | |
---|
24 | #include "Configuration.h" |
---|
25 | #include "File.h" |
---|
26 | #include "SVN.h" |
---|
27 | #include "SVNinfo.h" |
---|
28 | |
---|
29 | #include <cassert> |
---|
30 | #include <iostream> |
---|
31 | #include <fstream> |
---|
32 | #include <sstream> |
---|
33 | #include <string> |
---|
34 | #include <vector> |
---|
35 | |
---|
36 | std::vector<std::string> copyright_lines(std::istream&); |
---|
37 | |
---|
38 | int main( int argc, char* argv[]) |
---|
39 | { |
---|
40 | using namespace theplu::svndigest; |
---|
41 | bool verbose=false; |
---|
42 | bool ok=true; |
---|
43 | if (argc>1 && argv[1]==std::string("-v") ) |
---|
44 | verbose=true; |
---|
45 | |
---|
46 | std::string root=test::filename("toy_project"); |
---|
47 | std::string filename = root + "/README"; |
---|
48 | |
---|
49 | // Saving original file |
---|
50 | std::ifstream is(filename.c_str()); |
---|
51 | assert(is.good()); |
---|
52 | std::string original_file; |
---|
53 | std::getline(is, original_file, '\0'); |
---|
54 | is.close(); |
---|
55 | is.clear(std::ios::goodbit); |
---|
56 | |
---|
57 | is.open(filename.c_str()); |
---|
58 | std::vector<std::string> copyrights_old=copyright_lines(is); |
---|
59 | is.close(); |
---|
60 | is.clear(std::ios::goodbit); |
---|
61 | |
---|
62 | if (copyrights_old.size()!=1) { |
---|
63 | if (verbose){ |
---|
64 | std::cout << copyrights_old.size() << " Copyright lines\n"; |
---|
65 | for (size_t i=0; i<copyrights_old.size(); ++i) |
---|
66 | std::cout << copyrights_old[i] << "\n"; |
---|
67 | } |
---|
68 | ok = false; |
---|
69 | } |
---|
70 | else if (verbose) |
---|
71 | std::cout << "File contains 1 copyright line.\n"; |
---|
72 | |
---|
73 | // warn about missing Copyright statement only in verbose mode |
---|
74 | if (verbose){ |
---|
75 | std::string config_str("[copyright]\nmissing-copyright-warning=yes"); |
---|
76 | std::stringstream ss(config_str); |
---|
77 | Configuration& config = Configuration::instance(); |
---|
78 | config.load(ss); |
---|
79 | } |
---|
80 | |
---|
81 | if (verbose) |
---|
82 | std::cout << "Create SVN instance" << std::endl; |
---|
83 | SVN* svn=SVN::instance(root); |
---|
84 | if (!svn) |
---|
85 | return 1; |
---|
86 | |
---|
87 | // Extract repository location |
---|
88 | if (verbose) |
---|
89 | std::cout << "Extract repository location" << std::endl; |
---|
90 | std::string repo=SVNinfo(root).repos_root_url(); |
---|
91 | if (verbose) |
---|
92 | std::cout << "Create File object" << std::endl; |
---|
93 | File file(0,filename,""); |
---|
94 | |
---|
95 | if (verbose) |
---|
96 | std::cout << "Get stats for file" << std::endl; |
---|
97 | file.parse(verbose, true); |
---|
98 | |
---|
99 | if (verbose) |
---|
100 | std::cout << "Updating copyright statements" << std::endl; |
---|
101 | std::map<std::string, Alias> alias; |
---|
102 | alias["jari"]=Alias("jh", 1); |
---|
103 | alias["peter"]=Alias("pj", 2); |
---|
104 | |
---|
105 | file.print_copyright(alias, verbose); |
---|
106 | |
---|
107 | is.open(filename.c_str()); |
---|
108 | std::vector<std::string> copyrights=copyright_lines(is); |
---|
109 | is.close(); |
---|
110 | is.clear(std::ios::goodbit); |
---|
111 | |
---|
112 | std::vector<std::string> copyright_correct; |
---|
113 | copyright_correct.push_back("Copyright (C) 2006 jh"); |
---|
114 | copyright_correct.push_back("Copyright (C) 2007, 2008 pj"); |
---|
115 | if (copyrights.size()!=copyright_correct.size()) { |
---|
116 | ok = false; |
---|
117 | if (verbose) |
---|
118 | std::cout << "ERROR: expected " << copyright_correct.size() |
---|
119 | << " lines of Copyright (C)\n" |
---|
120 | << "But found " << copyrights.size() << " lines.\n"; |
---|
121 | } |
---|
122 | else { |
---|
123 | for (size_t i=0; i<copyrights.size(); ++i) |
---|
124 | if (copyrights[i]!=copyright_correct[i]){ |
---|
125 | ok=false; |
---|
126 | if (verbose) |
---|
127 | std::cerr << "ERROR: found '" << copyrights[i] << "'\n" |
---|
128 | << "expected: '" << copyright_correct[i] << "'\n"; |
---|
129 | } |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | // Restoring file |
---|
135 | std::ofstream os(filename.c_str()); |
---|
136 | assert(os.good()); |
---|
137 | os << original_file; |
---|
138 | os.close(); |
---|
139 | |
---|
140 | if (ok) { |
---|
141 | if (verbose) |
---|
142 | std::cout << "Test is Ok!" << std::endl; |
---|
143 | return 0; |
---|
144 | } |
---|
145 | if (verbose) |
---|
146 | std::cout << "Test failed." << std::endl; |
---|
147 | return 1; |
---|
148 | } |
---|
149 | |
---|
150 | std::vector<std::string> copyright_lines(std::istream& is) |
---|
151 | { |
---|
152 | using namespace theplu::svndigest; |
---|
153 | std::vector<std::string> res; |
---|
154 | std::string line; |
---|
155 | while (std::getline(is, line)){ |
---|
156 | if (match_begin(line.begin(), line.end(), "Copyright (C)")) |
---|
157 | res.push_back(line); |
---|
158 | } |
---|
159 | |
---|
160 | return res; |
---|
161 | } |
---|
162 | |
---|