File: test_arg_parse_version_check.h

package info (click to toggle)
seqan2 2.4.0%2Bdfsg-11~bpo9%2B1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 223,500 kB
  • sloc: cpp: 256,886; ansic: 91,672; python: 8,339; sh: 995; xml: 570; makefile: 251; awk: 51
file content (278 lines) | stat: -rw-r--r-- 12,699 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// ==========================================================================
//                 SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2018, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of Knut Reinert or the FU Berlin nor the names of
//       its contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: Svenja Mehringer <ssvenja.mehringer@fu-berlin.de>
// ==========================================================================

#ifndef SEQAN_TESTS_ARG_PARSE_TEST_ARG_PARSE_VERSION_CHECK_H_
#define SEQAN_TESTS_ARG_PARSE_TEST_ARG_PARSE_VERSION_CHECK_H_

#include <seqan/arg_parse.h>

#include <iostream>
#include <fstream>
#include <chrono>

struct TestVersionCheck_
{
    static const std::chrono::duration<long int>::rep TIME_NOW;

    static const std::string PATH; // See arg_parse_version_check.h for _getPath()
    static const std::string APP_NAME;
    static const std::string APP_VERSION_FILENAME;
    static const std::string APP_TIMESTAMP_FILENAME;

    static constexpr const char * const OPTION_VERSION_CHECK = "--version-check";
    static constexpr const char * const OPTION_OFF = "OFF";
    static constexpr const char * const OPTION_ON = "ON";
};

const std::chrono::duration<long int>::rep TestVersionCheck_::TIME_NOW = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
const std::string TestVersionCheck_::PATH = seqan::_getPath();
const std::string TestVersionCheck_::APP_NAME = "test_version_check_" + std::to_string(TIME_NOW); // avoid name conflicts
const std::string TestVersionCheck_::APP_VERSION_FILENAME     = TestVersionCheck_::PATH + "/" +
                                                                TestVersionCheck_::APP_NAME +
                                                                static_cast<std::string>(".version");
const std::string TestVersionCheck_::APP_TIMESTAMP_FILENAME   = TestVersionCheck_::PATH + "/" +
                                                                TestVersionCheck_::APP_NAME +
                                                                static_cast<std::string>("_usr.timestamp");

namespace seqan {

inline ArgumentParser::ParseResult _simulateArgumentParser(String<CharString> & stream_result,
                                                           bool & app_call_succeeded,
                                                           int argc,
                                                           const char ** argv)
{
    ArgumentParser parser;
    setAppName(parser, TestVersionCheck_::APP_NAME);
    setVersion(parser, "2.3.4");

    std::stringstream err_stream;
    std::stringstream out_stream;

    ArgumentParser::ParseResult res = parse(parser, argc, argv, out_stream, err_stream);

    appendValue(stream_result, out_stream.str());
    appendValue(stream_result, err_stream.str());

    // call future.get() to artificially wait for the thread to finish and avoid
    // any interference with following tests
    if(parser.appVersionCheckFuture.valid())
            app_call_succeeded = parser.appVersionCheckFuture.get();

    return res;
}

inline void _removeFilesFromPath()
{
    std::remove(TestVersionCheck_::APP_VERSION_FILENAME.c_str());
    std::remove(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str());
}

template <typename TMessage>
inline void _createFile(std::string const & filename, TMessage const & message)
{
    std::ofstream out_file(filename.c_str());
    if (out_file.is_open())
    {
        out_file << message;
        out_file.close();
    }
}

// even if the homedir might not be writable at least the tmp dir should be
SEQAN_DEFINE_TEST(test_path_availability)
{
    SEQAN_ASSERT(!TestVersionCheck_::PATH.empty()); // TODO:: correct when function is working!
}

// other tests might fail if the removal of intermediate results is not possible
SEQAN_DEFINE_TEST(test_delete_version_files)
{
    _removeFilesFromPath();
    SEQAN_ASSERT(!fileExists(TestVersionCheck_::APP_VERSION_FILENAME.c_str()));
    SEQAN_ASSERT(!fileExists(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str()));
}

SEQAN_DEFINE_TEST(test_create_files)
{
    _createFile(TestVersionCheck_::APP_VERSION_FILENAME.c_str(), "20.5.9");
    _createFile(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str(), TestVersionCheck_::TIME_NOW);

    SEQAN_ASSERT(fileExists(TestVersionCheck_::APP_VERSION_FILENAME.c_str()));
    SEQAN_ASSERT(fileExists(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str()));

    _removeFilesFromPath(); // clear files again
    SEQAN_ASSERT(!fileExists(TestVersionCheck_::APP_VERSION_FILENAME.c_str()));
    SEQAN_ASSERT(!fileExists(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str()));
}

SEQAN_DEFINE_TEST(test_option_on)
{
    int argc(3);
    const char * argv[3] = {TestVersionCheck_::APP_NAME.c_str(), TestVersionCheck_::OPTION_VERSION_CHECK,
                            TestVersionCheck_::OPTION_ON};
    String<CharString> stream_result;
    bool app_call_succeeded(false);

    ArgumentParser::ParseResult res = _simulateArgumentParser(stream_result, app_call_succeeded, argc, argv);

    SEQAN_ASSERT_EQ(res, ArgumentParser::PARSE_OK);
    SEQAN_ASSERT_EQ(stream_result[0], "");
    SEQAN_ASSERT_EQ(stream_result[1], "");

    // make sure that all files now exist
    // SEQAN_ASSERT(fileExists(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str()));
    if (app_call_succeeded)
        SEQAN_ASSERT(fileExists(TestVersionCheck_::APP_VERSION_FILENAME.c_str()));
}

SEQAN_DEFINE_TEST(test_option_off)
{
    int argc(3);
    const char * argv[3] = {TestVersionCheck_::APP_NAME.c_str(), TestVersionCheck_::OPTION_VERSION_CHECK,
                            TestVersionCheck_::OPTION_OFF};
    String<CharString> stream_result;
    bool app_call_succeeded(false);

    ArgumentParser::ParseResult res = _simulateArgumentParser(stream_result, app_call_succeeded, argc, argv);

    SEQAN_ASSERT_EQ(res, ArgumentParser::PARSE_OK);
    SEQAN_ASSERT_EQ(stream_result[0], "");
    SEQAN_ASSERT_EQ(stream_result[1], "");

    // make sure that no files exist
    SEQAN_ASSERT(!fileExists(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str()));
    SEQAN_ASSERT(!fileExists(TestVersionCheck_::APP_VERSION_FILENAME.c_str()));
}

// case: the current argument parser has a smaller seqan version than is present in the version file
SEQAN_DEFINE_TEST(test_smaller_seqan_version)
{
    int argc(3);
    const char * argv[3] = {TestVersionCheck_::APP_NAME.c_str(), TestVersionCheck_::OPTION_VERSION_CHECK,
                            TestVersionCheck_::OPTION_ON};
    String<CharString> stream_result;
    bool app_call_succeeded(false);

    // create version file with euqal app version and a greater seqan version than the current (2.3.4)
    std::stringstream version_info;
    version_info << "2.3.4" << std::endl << "20.5.9";
    _createFile(TestVersionCheck_::APP_VERSION_FILENAME.c_str(), version_info.str());

    // create timestamp file that dates one day before current to trigger a message
    _createFile(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str(), TestVersionCheck_::TIME_NOW - 86401); // one day = 86400 seconds

    ArgumentParser::ParseResult res = _simulateArgumentParser(stream_result, app_call_succeeded, argc, argv);

    SEQAN_ASSERT_EQ(res, ArgumentParser::PARSE_OK);
    SEQAN_ASSERT_EQ(stream_result[0], "");
    SEQAN_ASSERT_EQ(stream_result[1], VersionControlTags_<>::MESSAGE_SEQAN_UPDATE);
}

// case: the current argument parser has a smaller app version than is present in the version file
SEQAN_DEFINE_TEST(test_smaller_app_version)
{
    int argc(3);
    const char * argv[3] = {TestVersionCheck_::APP_NAME.c_str(), TestVersionCheck_::OPTION_VERSION_CHECK,
                            TestVersionCheck_::OPTION_ON};
    String<CharString> stream_result;
    bool app_call_succeeded(false);

    // create version file with equal seqan version and a greater app version than the current (2.3.4)
    std::string seqan_version = std::to_string(SEQAN_VERSION_MAJOR) + "." +
                                std::to_string(SEQAN_VERSION_MINOR) + "." +
                                std::to_string(SEQAN_VERSION_PATCH);
    std::stringstream version_info;
    version_info << "20.5.9" << std::endl << seqan_version;
    _createFile(TestVersionCheck_::APP_VERSION_FILENAME.c_str(), version_info.str());

    // create timestamp file that dates one day before current to trigger a message
    _createFile(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str(), TestVersionCheck_::TIME_NOW - 86401); // one day = 86400 seconds

    ArgumentParser::ParseResult res = _simulateArgumentParser(stream_result, app_call_succeeded, argc, argv);

    SEQAN_ASSERT_EQ(res, ArgumentParser::PARSE_OK);
    SEQAN_ASSERT_EQ(stream_result[0], "");
    SEQAN_ASSERT_EQ(stream_result[1], VersionControlTags_<>::MESSAGE_APP_UPDATE);
}

// case: the current argument parser has a greater app version than is present in the version file
SEQAN_DEFINE_TEST(test_greater_app_version)
{
    int argc(3);
    const char * argv[3] = {TestVersionCheck_::APP_NAME.c_str(), TestVersionCheck_::OPTION_VERSION_CHECK,
                            TestVersionCheck_::OPTION_ON};
    String<CharString> stream_result;
    bool app_call_succeeded(false);

    // create version file with equal seqan version and a smaller app version than the current (2.3.4)
    std::string seqan_version = std::to_string(SEQAN_VERSION_MAJOR) + "." +
                                std::to_string(SEQAN_VERSION_MINOR) + "." +
                                std::to_string(SEQAN_VERSION_PATCH);
    std::stringstream version_info;
    version_info << "1.5.9" << std::endl << seqan_version;
    _createFile(TestVersionCheck_::APP_VERSION_FILENAME.c_str(), version_info.str());

    // create timestamp file that dates one day before current to trigger a message
    _createFile(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str(), TestVersionCheck_::TIME_NOW - 86401); // one day = 86400 seconds
 
    ArgumentParser::ParseResult res = _simulateArgumentParser(stream_result, app_call_succeeded, argc, argv);

    SEQAN_ASSERT_EQ(res, ArgumentParser::PARSE_OK);
    SEQAN_ASSERT_EQ(stream_result[0], "");
    SEQAN_ASSERT_EQ(stream_result[1], VersionControlTags_<>::MESSAGE_REGISTERED_APP_UPDATE);
}

SEQAN_DEFINE_TEST(test_time_out)
{
    int argc(3);
    const char * argv[3] = {TestVersionCheck_::APP_NAME.c_str(), TestVersionCheck_::OPTION_VERSION_CHECK,
                            TestVersionCheck_::OPTION_ON};
    String<CharString> stream_result;
    bool app_call_succeeded(false);

    // create timestamp files
    _createFile(TestVersionCheck_::APP_TIMESTAMP_FILENAME.c_str(), TestVersionCheck_::TIME_NOW);

    ArgumentParser::ParseResult res = _simulateArgumentParser(stream_result, app_call_succeeded, argc, argv);

    SEQAN_ASSERT_EQ(res, ArgumentParser::PARSE_OK);
    SEQAN_ASSERT_EQ(stream_result[0], "");
    SEQAN_ASSERT_EQ(stream_result[1], "");

    SEQAN_ASSERT(!fileExists(TestVersionCheck_::APP_VERSION_FILENAME.c_str()));
}

} // namespace seqan

#endif  // SEQAN_TESTS_ARG_PARSE_TEST_ARG_PARSE_VERSION_CHECK_H_