40#include <wx/filename.h>
83 std::string expression;
87 const std::vector<TestCase> cases = {
88 {
"@{vcsidentifier()}", 40 },
89 {
"@{vcsidentifier(40)}", 40 },
90 {
"@{vcsidentifier(7)}", 7 },
91 {
"@{vcsidentifier(8)}", 8 },
92 {
"@{vcsidentifier(12)}", 12 },
93 {
"@{vcsidentifier(4)}", 4 },
95 {
"@{vcsfileidentifier(\".\")}", 40 },
96 {
"@{vcsfileidentifier(\".\", 8)}", 8 },
99 std::regex hexPattern(
"^[0-9a-f]+$" );
101 for(
const auto& testCase : cases )
103 auto result = evaluator.
Evaluate( wxString::FromUTF8( testCase.expression ) );
105 BOOST_CHECK_MESSAGE( !evaluator.
HasErrors(),
106 "Error in expression: " + testCase.expression +
" Errors: "
121 auto branch = evaluator.
Evaluate(
"@{vcsbranch()}" );
125 auto authorEmail = evaluator.
Evaluate(
"@{vcsauthoremail()}" );
129 auto committerEmail = evaluator.
Evaluate(
"@{vcscommitteremail()}" );
133 auto author = evaluator.
Evaluate(
"@{vcsauthor()}" );
137 auto committer = evaluator.
Evaluate(
"@{vcscommitter()}" );
142 auto fileAuthorEmail = evaluator.
Evaluate(
"@{vcsfileauthoremail(\".\")}" );
146 auto fileCommitterEmail = evaluator.
Evaluate(
"@{vcsfilecommitteremail(\".\")}" );
160 std::string expression;
163 const std::vector<TestCase> cases = {
165 {
"@{vcsdirty(0)}" },
166 {
"@{vcsdirty(1)}" },
169 for(
const auto& testCase : cases )
171 auto result = evaluator.
Evaluate( wxString::FromUTF8( testCase.expression ) );
185 const std::vector<std::string> cases = {
186 "@{vcsdirtysuffix()}",
187 "@{vcsdirtysuffix(\"-modified\")}",
188 "@{vcsdirtysuffix(\"+\", 1)}",
191 for(
const auto& expr : cases )
193 evaluator.
Evaluate( wxString::FromUTF8( expr ) );
205 const std::vector<std::string> cases = {
206 "@{vcsnearestlabel()}",
207 "@{vcsnearestlabel(\"\")}",
208 "@{vcsnearestlabel(\"v*\")}",
209 "@{vcsnearestlabel(\"\", 0)}",
210 "@{vcsnearestlabel(\"\", 1)}",
212 "@{vcslabeldistance()}",
213 "@{vcslabeldistance(\"v*\")}",
214 "@{vcslabeldistance(\"\", 1)}",
217 std::regex numberPattern(
"^[0-9]+$" );
219 for(
const auto& expr : cases )
224 if( !
result.IsEmpty() && expr.find(
"distance" ) != std::string::npos )
240 std::string expression;
244 const std::vector<TestCase> cases = {
245 {
"@{vcscommitdate()}", std::regex(
"^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ) },
246 {
"@{vcscommitdate(\"ISO\")}", std::regex(
"^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ) },
247 {
"@{vcscommitdate(\"US\")}", std::regex(
"^[0-9]{2}/[0-9]{2}/[0-9]{4}$" ) },
248 {
"@{vcscommitdate(\"EU\")}", std::regex(
"^[0-9]{2}/[0-9]{2}/[0-9]{4}$" ) },
250 {
"@{vcsfilecommitdate(\".\")}", std::regex(
"^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ) },
253 for(
const auto& testCase : cases )
255 auto result = evaluator.
Evaluate( wxString::FromUTF8( testCase.expression ) );
257 BOOST_CHECK_MESSAGE( !evaluator.
HasErrors(),
258 "Error in expression: " + testCase.expression +
" Errors: "
261 BOOST_CHECK_MESSAGE( std::regex_match(
result.ToStdString(), testCase.pattern ),
262 "Bad date format for " + testCase.expression +
": "
274 auto start = std::chrono::high_resolution_clock::now();
276 for(
int i = 0; i < 100; ++i )
282 auto end = std::chrono::high_resolution_clock::now();
283 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
end - start );
286#if defined( KICAD_SANITIZE_THREADS ) || defined( KICAD_SANITIZE_ADDRESS )
289 BOOST_CHECK_LT( duration.count(), 2000 );
299 evaluator.
SetVariable( wxString(
"PROJECT" ), wxString(
"MyProject" ) );
301 const std::vector<std::string> cases = {
302 "Version: @{vcsbranch()}",
303 "Commit: @{vcsidentifier(7)}",
304 "Author: @{vcsauthor()} <@{vcsauthoremail()}>",
306 "${PROJECT} @{vcsbranch()}",
307 "Built from @{vcsnearestlabel()}@{vcsdirtysuffix()}",
309 "Distance: @{vcslabeldistance() + 0}",
312 for(
const auto& expr : cases )
332 wxString scratchDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
333 + wxString::Format(
"kicad_vcs_cli_%ld", wxGetProcessId() );
335 wxFileName::Mkdir( scratchDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
336 wxSetWorkingDirectory( scratchDir );
338 auto cleanupCwd = [&]()
340 wxSetWorkingDirectory( tempDir() );
342 if( wxFileName::DirExists( scratchDir ) )
343 wxFileName::Rmdir( scratchDir, wxPATH_RMDIR_RECURSIVE );
351 auto noContext = evaluator.
Evaluate(
"@{vcsidentifier(7)}" );
359 auto hash = evaluator.
Evaluate(
"@{vcsidentifier(7)}" );
363 std::regex hexPattern(
"^[0-9a-f]+$" );
364 BOOST_CHECK( std::regex_match( hash.ToStdString(), hexPattern ) );
366 auto branch = evaluator.
Evaluate(
"@{vcsbranch()}" );
371 auto author = evaluator.
Evaluate(
"@{vcsauthor()}" );
377 auto afterScope = evaluator.
Evaluate(
"@{vcsidentifier(7)}" );
397 wxString scratchDir = wxFileName::GetTempDir() + wxFileName::GetPathSeparator()
398 + wxString::Format(
"kicad_vcs_loadproject_%ld", wxGetProcessId() );
400 wxFileName::Mkdir( scratchDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
401 wxSetWorkingDirectory( scratchDir );
405 wxString projectFile = tempDir() + wxFileName::GetPathSeparator()
406 + wxT(
"issue23959.kicad_pro" );
409 std::ofstream f( projectFile.ToStdString() );
410 f <<
"{ \"meta\": { \"filename\": \"issue23959.kicad_pro\", \"version\": 3 } }";
415 wxRemoveFile( projectFile );
416 wxSetWorkingDirectory( tempDir() );
418 if( wxFileName::DirExists( scratchDir ) )
419 wxFileName::Rmdir( scratchDir, wxPATH_RMDIR_RECURSIVE );
426 auto baseline = evaluator.
Evaluate(
"@{vcsidentifier(7)}" );
429 BOOST_REQUIRE(
Pgm().GetSettingsManager().LoadProject( projectFile,
true ) );
432 auto loaded = evaluator.
Evaluate(
"@{vcsidentifier(7)}" );
436 std::regex hexPattern(
"^[0-9a-f]+$" );
437 BOOST_CHECK( std::regex_match( loaded.ToStdString(), hexPattern ) );
444 auto afterUnload = evaluator.
Evaluate(
"@{vcsidentifier(7)}" );
460 wxString projectPath = tempDir() + wxFileName::GetPathSeparator() + wxT(
"ctx.kicad_pro" );
463 std::ofstream f( projectPath.ToStdString() );
464 f <<
"{\"meta\": {\"filename\": \"ctx.kicad_pro\", \"version\": 3}}";
469 BOOST_TEST_REQUIRE( mgr.
LoadProject( projectPath ) );
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.
Give each test a backend and a temporary directory.
const wxString & TempPath() const
virtual SETTINGS_MANAGER & GetSettingsManager() const
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Save, unload and unregister the given PROJECT.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true, bool aOverrideLock=false)
Load a project or sets up a new project with a specified path.
RAII helper that sets the VCS context path on construction and restores the previous value on destruc...
static std::string ToStdString(const wxString &aStr)
std::unique_ptr< git_repository, decltype([](git_repository *aRepo) { git_repository_free(aRepo); })> GitRepositoryPtr
A unique pointer for git_repository objects with automatic cleanup.
KIGIT::GitRepositoryPtr InitGitRepository(const wxString &aPath, const char *aBranch="main", const char *aAuthorName="QA Test", const char *aAuthorEmail="
[email protected]")
Pin the branch and author so repository setup does not depend on the user's Git config.
void WriteGitFile(const wxString &aRepoPath, const wxString &aRelativePath, const std::string &aContents)
Write test data without staging it. Create parent directories when needed.
git_oid CommitGitFile(git_repository *aRepo, const wxString &aPath, const wxString &aMessage)
Stage one file and commit the index. Other staged changes are kept.
wxString GetContextPath()
Return the current context path for repo-scoped VCS queries.
PGM_BASE & Pgm()
The global program "get" accessor.
Fixture that creates a temporary git repo with one committed file.
TEXT_EVAL_VCS::CONTEXT_PATH_SCOPE m_context
const wxString & tempDir() const
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
static const char * TEST_AUTHOR_NAME
static const char * TEST_AUTHOR_EMAIL
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
wxString result
Test unit parsing edge cases and error handling.
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