source: trunk/test/copyright.cc @ 595

Last change on this file since 595 was 595, checked in by Peter Johansson, 13 years ago

working on copyright test. As now the test fails and is therefore tagged under XFAIL_TESTS.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1// $Id: copyright.cc 595 2008-04-14 12:10:42Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of svndigest, http://trac.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 "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 <string>
33#include <vector>
34
35std::vector<std::string> copyright_lines(std::istream&);
36
37int main( int argc, char* argv[])
38{
39  using namespace theplu::svndigest;
40  bool verbose=false;
41  bool ok=true;
42  if (argc>1 && argv[1]==std::string("-v") )
43    verbose=true;
44
45  std::string root("toy_project");
46  std::string filename = root + "/README";
47
48  // Saving original file
49  std::ifstream is(filename.c_str());
50  assert(is.good());
51  std::string original_file;
52  std::getline(is, original_file, '\0');
53  is.close();
54  is.clear(std::ios::goodbit);
55
56  is.open(filename.c_str());
57  std::vector<std::string> copyrights_old=copyright_lines(is);
58  is.close();
59  is.clear(std::ios::goodbit);
60
61  if (copyrights_old.size()!=1) {
62    if (verbose){
63      std::cout << copyrights_old.size() << " Copyright lines\n";
64      for (size_t i=0; i<copyrights_old.size(); ++i)
65        std::cout << copyrights_old[i] << "\n";
66    }
67    ok = false;
68  }
69  else if (verbose)
70    std::cout << "File containes 1 copyright line.\n"; 
71
72  if (verbose)
73    std::cout << "Create SVN instance" << std::endl;
74  SVN* svn=SVN::instance(root);
75  if (!svn)
76    return 1;
77
78  // Extract repository location
79  if (verbose)
80    std::cout << "Extract repository location" << std::endl;
81  std::string repo=SVNinfo(root).repos_root_url();
82  if (verbose)
83    std::cout << "Create File object" << std::endl;
84  File file(0,filename,"");
85 
86  if (verbose)
87    std::cout << "Get stats for file" << std::endl;
88  file.parse(verbose, true);
89 
90  if (verbose)
91    std::cout << "Updating copyright statements" << std::endl;
92  std::map<std::string, Alias> alias;
93  file.print_copyright(alias, verbose);
94
95  is.open(filename.c_str());
96  std::vector<std::string> copyrights=copyright_lines(is);
97  is.close();
98  is.clear(std::ios::goodbit);
99
100  if (copyrights.size()!=2) {
101    ok = false;
102    if (verbose)
103      std::cout << "ERROR: expected two lines of Copyright (C)\n";
104  }
105  if (verbose) {
106    std::cout << copyrights.size() << " Copyright lines\n";
107    for (size_t i=0; i<copyrights.size(); ++i)
108      std::cout << copyrights[i] << "\n";
109  }
110
111  // Restoring file
112  std::ofstream os(filename.c_str());
113  assert(os.good());
114  os << original_file;
115
116  if (ok) {
117    if (verbose)
118      std::cout << "Test is Ok!" << std::endl;
119    return 0;
120  }
121  if (verbose)
122    std::cout << "Test failed." << std::endl;
123  return 1;
124}
125
126std::vector<std::string> copyright_lines(std::istream& is)
127{
128  using namespace theplu::svndigest;
129  std::vector<std::string> res;
130  std::string line;
131  while (std::getline(is, line)){
132    if (match_begin(line.begin(), line.end(), "Copyright (C)"))
133      res.push_back(line);
134  }
135 
136  return res;
137}
138
Note: See TracBrowser for help on using the repository browser.