1 | // $Id: copyright_test.cc 687 2008-08-04 19:37:10Z peter $ |
---|
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 2 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 this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "Suite.h" |
---|
25 | |
---|
26 | #include "Configuration.h" |
---|
27 | #include "File.h" |
---|
28 | #include "SVN.h" |
---|
29 | #include "SVNinfo.h" |
---|
30 | |
---|
31 | #include <cassert> |
---|
32 | #include <iostream> |
---|
33 | #include <fstream> |
---|
34 | #include <sstream> |
---|
35 | #include <string> |
---|
36 | #include <vector> |
---|
37 | |
---|
38 | std::vector<std::string> copyright_lines(std::istream&); |
---|
39 | |
---|
40 | int main( int argc, char* argv[]) |
---|
41 | { |
---|
42 | using namespace theplu::svndigest; |
---|
43 | bool verbose=false; |
---|
44 | bool ok=true; |
---|
45 | if (argc>1 && argv[1]==std::string("-v") ) |
---|
46 | verbose=true; |
---|
47 | |
---|
48 | std::string root=test::filename("toy_project"); |
---|
49 | std::string filename = root + "/README"; |
---|
50 | |
---|
51 | // Saving original file |
---|
52 | std::ifstream is(filename.c_str()); |
---|
53 | assert(is.good()); |
---|
54 | std::string original_file; |
---|
55 | std::getline(is, original_file, '\0'); |
---|
56 | is.close(); |
---|
57 | is.clear(std::ios::goodbit); |
---|
58 | |
---|
59 | is.open(filename.c_str()); |
---|
60 | std::vector<std::string> copyrights_old=copyright_lines(is); |
---|
61 | is.close(); |
---|
62 | is.clear(std::ios::goodbit); |
---|
63 | |
---|
64 | if (copyrights_old.size()!=1) { |
---|
65 | if (verbose){ |
---|
66 | std::cout << copyrights_old.size() << " Copyright lines\n"; |
---|
67 | for (size_t i=0; i<copyrights_old.size(); ++i) |
---|
68 | std::cout << copyrights_old[i] << "\n"; |
---|
69 | } |
---|
70 | ok = false; |
---|
71 | } |
---|
72 | else if (verbose) |
---|
73 | std::cout << "File contains 1 copyright line.\n"; |
---|
74 | |
---|
75 | // warn about missing Copyright statement only in verbose mode |
---|
76 | if (verbose){ |
---|
77 | std::string config_str("[copyright]\nmissing-copyright-warning=yes"); |
---|
78 | std::stringstream ss(config_str); |
---|
79 | Configuration& config = Configuration::instance(); |
---|
80 | config.load(ss); |
---|
81 | } |
---|
82 | |
---|
83 | if (verbose) |
---|
84 | std::cout << "Create SVN instance" << std::endl; |
---|
85 | SVN* svn=SVN::instance(root); |
---|
86 | if (!svn) |
---|
87 | return 1; |
---|
88 | |
---|
89 | // Extract repository location |
---|
90 | if (verbose) |
---|
91 | std::cout << "Extract repository location" << std::endl; |
---|
92 | std::string repo=SVNinfo(root).repos_root_url(); |
---|
93 | if (verbose) |
---|
94 | std::cout << "Create File object" << std::endl; |
---|
95 | File file(0,filename,""); |
---|
96 | |
---|
97 | if (verbose) |
---|
98 | std::cout << "Get stats for file" << std::endl; |
---|
99 | file.parse(verbose, true); |
---|
100 | |
---|
101 | if (verbose) |
---|
102 | std::cout << "Updating copyright statements" << std::endl; |
---|
103 | std::map<std::string, Alias> alias; |
---|
104 | alias["jari"]=Alias("jh", 1); |
---|
105 | alias["peter"]=Alias("pj", 2); |
---|
106 | |
---|
107 | file.print_copyright(alias, verbose); |
---|
108 | |
---|
109 | is.open(filename.c_str()); |
---|
110 | std::vector<std::string> copyrights=copyright_lines(is); |
---|
111 | is.close(); |
---|
112 | is.clear(std::ios::goodbit); |
---|
113 | |
---|
114 | std::vector<std::string> copyright_correct; |
---|
115 | copyright_correct.push_back("Copyright (C) 2006 jh"); |
---|
116 | copyright_correct.push_back("Copyright (C) 2007, 2008 pj"); |
---|
117 | if (copyrights.size()!=copyright_correct.size()) { |
---|
118 | ok = false; |
---|
119 | if (verbose) |
---|
120 | std::cout << "ERROR: expected " << copyright_correct.size() |
---|
121 | << " lines of Copyright (C)\n" |
---|
122 | << "But found " << copyrights.size() << " lines.\n"; |
---|
123 | } |
---|
124 | else { |
---|
125 | for (size_t i=0; i<copyrights.size(); ++i) |
---|
126 | if (copyrights[i]!=copyright_correct[i]){ |
---|
127 | ok=false; |
---|
128 | if (verbose) |
---|
129 | std::cerr << "ERROR: found '" << copyrights[i] << "'\n" |
---|
130 | << "expected: '" << copyright_correct[i] << "'\n"; |
---|
131 | } |
---|
132 | |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | // Restoring file |
---|
137 | std::ofstream os(filename.c_str()); |
---|
138 | assert(os.good()); |
---|
139 | os << original_file; |
---|
140 | os.close(); |
---|
141 | |
---|
142 | if (ok) { |
---|
143 | if (verbose) |
---|
144 | std::cout << "Test is Ok!" << std::endl; |
---|
145 | return 0; |
---|
146 | } |
---|
147 | if (verbose) |
---|
148 | std::cout << "Test failed." << std::endl; |
---|
149 | return 1; |
---|
150 | } |
---|
151 | |
---|
152 | std::vector<std::string> copyright_lines(std::istream& is) |
---|
153 | { |
---|
154 | using namespace theplu::svndigest; |
---|
155 | std::vector<std::string> res; |
---|
156 | std::string line; |
---|
157 | while (std::getline(is, line)){ |
---|
158 | if (match_begin(line.begin(), line.end(), "Copyright (C)")) |
---|
159 | res.push_back(line); |
---|
160 | } |
---|
161 | |
---|
162 | return res; |
---|
163 | } |
---|
164 | |
---|