43#include <wx/filename.h>
66 m_tempDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
67 + wxString::Format(
"kicad_vcs_test_%ld", wxGetProcessId() );
69 wxFileName::Mkdir(
m_tempDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
82 wxFileName::Rmdir(
m_tempDir, wxPATH_RMDIR_RECURSIVE );
94 git_repository* repo =
nullptr;
96 if( git_repository_init( &repo,
m_tempDir.ToUTF8().data(), 0 ) != 0 )
100 git_config*
config =
nullptr;
102 if( git_repository_config( &
config, repo ) == 0 )
106 git_config_free(
config );
110 wxString filePath =
m_tempDir + wxFileName::GetPathSeparator() + wxT(
"test.txt" );
113 std::ofstream f( filePath.ToStdString() );
114 f <<
"test content\n";
118 git_index*
index =
nullptr;
120 if( git_repository_index( &
index, repo ) != 0 )
122 git_repository_free( repo );
126 git_index_add_bypath(
index,
"test.txt" );
127 git_index_write(
index );
132 if( git_index_write_tree( &treeOid,
index ) != 0 )
134 git_index_free(
index );
135 git_repository_free( repo );
139 git_index_free(
index );
141 git_tree* tree =
nullptr;
143 if( git_tree_lookup( &tree, repo, &treeOid ) != 0 )
145 git_repository_free( repo );
150 git_signature* sig =
nullptr;
154 git_tree_free( tree );
155 git_repository_free( repo );
160 int err = git_commit_create_v( &commitOid, repo,
"HEAD", sig, sig,
nullptr,
163 git_signature_free( sig );
164 git_tree_free( tree );
165 git_repository_free( repo );
183 BOOST_TEST_REQUIRE( repoReady() );
189 std::string expression;
193 const std::vector<TestCase> cases = {
194 {
"@{vcsidentifier()}", 40 },
195 {
"@{vcsidentifier(40)}", 40 },
196 {
"@{vcsidentifier(7)}", 7 },
197 {
"@{vcsidentifier(8)}", 8 },
198 {
"@{vcsidentifier(12)}", 12 },
199 {
"@{vcsidentifier(4)}", 4 },
201 {
"@{vcsfileidentifier(\".\")}", 40 },
202 {
"@{vcsfileidentifier(\".\", 8)}", 8 },
205 std::regex hexPattern(
"^[0-9a-f]+$" );
207 for(
const auto& testCase : cases )
209 auto result = evaluator.
Evaluate( wxString::FromUTF8( testCase.expression ) );
211 BOOST_CHECK_MESSAGE( !evaluator.
HasErrors(),
212 "Error in expression: " + testCase.expression +
" Errors: "
216 BOOST_CHECK( std::regex_match(
result.ToStdString(), hexPattern ) );
225 BOOST_TEST_REQUIRE( repoReady() );
229 auto branch = evaluator.
Evaluate(
"@{vcsbranch()}" );
231 BOOST_CHECK( !branch.IsEmpty() );
233 auto authorEmail = evaluator.
Evaluate(
"@{vcsauthoremail()}" );
237 auto committerEmail = evaluator.
Evaluate(
"@{vcscommitteremail()}" );
241 auto author = evaluator.
Evaluate(
"@{vcsauthor()}" );
245 auto committer = evaluator.
Evaluate(
"@{vcscommitter()}" );
250 auto fileAuthorEmail = evaluator.
Evaluate(
"@{vcsfileauthoremail(\".\")}" );
254 auto fileCommitterEmail = evaluator.
Evaluate(
"@{vcsfilecommitteremail(\".\")}" );
264 BOOST_TEST_REQUIRE( repoReady() );
270 std::string expression;
273 const std::vector<TestCase> cases = {
275 {
"@{vcsdirty(0)}" },
276 {
"@{vcsdirty(1)}" },
279 for(
const auto& testCase : cases )
281 auto result = evaluator.
Evaluate( wxString::FromUTF8( testCase.expression ) );
293 BOOST_TEST_REQUIRE( repoReady() );
297 const std::vector<std::string> cases = {
298 "@{vcsdirtysuffix()}",
299 "@{vcsdirtysuffix(\"-modified\")}",
300 "@{vcsdirtysuffix(\"+\", 1)}",
303 for(
const auto& expr : cases )
305 evaluator.
Evaluate( wxString::FromUTF8( expr ) );
315 BOOST_TEST_REQUIRE( repoReady() );
319 const std::vector<std::string> cases = {
320 "@{vcsnearestlabel()}",
321 "@{vcsnearestlabel(\"\")}",
322 "@{vcsnearestlabel(\"v*\")}",
323 "@{vcsnearestlabel(\"\", 0)}",
324 "@{vcsnearestlabel(\"\", 1)}",
326 "@{vcslabeldistance()}",
327 "@{vcslabeldistance(\"v*\")}",
328 "@{vcslabeldistance(\"\", 1)}",
331 std::regex numberPattern(
"^[0-9]+$" );
333 for(
const auto& expr : cases )
338 if( !
result.IsEmpty() && expr.find(
"distance" ) != std::string::npos )
340 BOOST_CHECK( std::regex_match(
result.ToStdString(), numberPattern ) );
350 BOOST_TEST_REQUIRE( repoReady() );
356 std::string expression;
360 const std::vector<TestCase> cases = {
361 {
"@{vcscommitdate()}", std::regex(
"^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ) },
362 {
"@{vcscommitdate(\"ISO\")}", std::regex(
"^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ) },
363 {
"@{vcscommitdate(\"US\")}", std::regex(
"^[0-9]{2}/[0-9]{2}/[0-9]{4}$" ) },
364 {
"@{vcscommitdate(\"EU\")}", std::regex(
"^[0-9]{2}/[0-9]{2}/[0-9]{4}$" ) },
366 {
"@{vcsfilecommitdate(\".\")}", std::regex(
"^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ) },
369 for(
const auto& testCase : cases )
371 auto result = evaluator.
Evaluate( wxString::FromUTF8( testCase.expression ) );
373 BOOST_CHECK_MESSAGE( !evaluator.
HasErrors(),
374 "Error in expression: " + testCase.expression +
" Errors: "
377 BOOST_CHECK_MESSAGE( std::regex_match(
result.ToStdString(), testCase.pattern ),
378 "Bad date format for " + testCase.expression +
": "
388 BOOST_TEST_REQUIRE( repoReady() );
392 auto start = std::chrono::high_resolution_clock::now();
394 for(
int i = 0; i < 100; ++i )
400 auto end = std::chrono::high_resolution_clock::now();
401 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end - start );
403 BOOST_CHECK_LT( duration.count(), 2000 );
411 BOOST_TEST_REQUIRE( repoReady() );
414 evaluator.
SetVariable( wxString(
"PROJECT" ), wxString(
"MyProject" ) );
416 const std::vector<std::string> cases = {
417 "Version: @{vcsbranch()}",
418 "Commit: @{vcsidentifier(7)}",
419 "Author: @{vcsauthor()} <@{vcsauthoremail()}>",
421 "${PROJECT} @{vcsbranch()}",
422 "Built from @{vcsnearestlabel()}@{vcsdirtysuffix()}",
424 "Distance: @{vcslabeldistance() + 0}",
427 for(
const auto& expr : cases )
432 BOOST_CHECK( !
result.IsEmpty() );
High-level wrapper for evaluating mathematical and string expressions in wxString format.
wxString Evaluate(const wxString &aInput)
Main evaluation function - processes input string and evaluates all} expressions.
bool HasErrors() const
Check if the last evaluation had errors.
wxString GetErrorSummary() const
Get detailed error information from the last evaluation.
void SetVariable(const wxString &aName, double aValue)
Set a numeric variable for use in expressions.
void SetGitBackend(GIT_BACKEND *aBackend)
Fixture that creates a temporary git repo with one committed file.
LIBGIT_BACKEND * m_backend
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")
static const char * TEST_AUTHOR_NAME
static const char * TEST_AUTHOR_EMAIL
BOOST_AUTO_TEST_CASE(VcsIdentifierFormatting)
Test VCS identifier functions with various lengths.
static const char * TEST_COMMIT_MSG