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