KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_jobs_runner_record_output.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <qa_utils/file_utils.h>
23
25#include <jobs/jobset.h>
26#include <jobs/jobs_output.h>
27#include <jobs_runner.h>
28#include <reporter.h>
29
30#include <wx/log.h>
31
32#include <filesystem>
33#include <fstream>
34
35
36namespace
37{
38namespace fs = std::filesystem;
39
40// An Execute Command job that records its output, run into a folder destination
41struct RECORD_OUTPUT_FIXTURE
42{
43 RECORD_OUTPUT_FIXTURE() :
44 m_destDir( wxS( "kicad_jobs_runner_record" ) ),
45 m_jobset( wxString::FromUTF8( ( m_destDir.Path() / "record.kicad_jobset" ).string().c_str() ) )
46 {
47 m_execute = new JOB_SPECIAL_EXECUTE();
48 m_execute->m_command = wxS( "echo recorded" );
49 m_execute->m_recordOutput = true;
50 m_execute->SetConfiguredOutputPath( wxS( "data/" ) );
51
52 m_jobset.AddNewJob( wxS( "special_execute" ), m_execute );
53 m_job = &m_jobset.GetJobs().front();
54
55 JOBSET_DESTINATION* destination = m_jobset.AddNewDestination( JOBSET_DESTINATION_T::FOLDER );
56 destination->m_only = { m_job->m_id };
57 destination->m_outputHandler->FromJson( { { "output_path", ( m_destDir.Path() / "out" ).string() } } );
58 }
59
60 bool Run( WX_STRING_REPORTER& aReporter )
61 {
62 JOBS_RUNNER runner( nullptr, &m_jobset, nullptr, aReporter, nullptr );
63
64 return runner.RunJobsForDestination( &m_jobset.GetDestinations().front() );
65 }
66
67 KI_TEST::SCOPED_TEMP_DIR m_destDir;
68 JOBSET m_jobset;
69 JOBSET_JOB* m_job = nullptr;
70 JOB_SPECIAL_EXECUTE* m_execute = nullptr;
71};
72} // namespace
73
74
75BOOST_FIXTURE_TEST_SUITE( JobsRunnerRecordOutput, RECORD_OUTPUT_FIXTURE )
76
77
78BOOST_AUTO_TEST_CASE( ExecuteCommandRecordOutputIntoFolder )
79{
81
82 BOOST_CHECK( Run( reporter ) );
83
84 fs::path logFile = m_destDir.Path() / "out" / "data" / ( m_job->m_id.ToStdString() + ".log" );
85 BOOST_REQUIRE( fs::is_regular_file( logFile ) );
86
87 std::ifstream log( logFile );
88 std::string line;
89 std::getline( log, line );
90 BOOST_CHECK( line.starts_with( "recorded" ) );
91}
92
93
94// The default log name is not written back into the job a user saves
95BOOST_AUTO_TEST_CASE( ExecuteCommandDefaultLogNotStored )
96{
97 m_execute->SetConfiguredOutputPath( wxEmptyString );
98
100
101 BOOST_CHECK( Run( reporter ) );
102 BOOST_CHECK( fs::is_regular_file( m_destDir.Path() / "out" / ( m_job->m_id.ToStdString() + ".log" ) ) );
103 BOOST_CHECK_MESSAGE( m_execute->GetConfiguredOutputPath().IsEmpty(), m_execute->GetConfiguredOutputPath() );
104}
105
106
107// A log that cannot be created is named in the error
108BOOST_AUTO_TEST_CASE( ExecuteCommandRecordOutputFailureNamesLog )
109{
110 fs::path blocker = m_destDir.Path() / "blocker";
111 std::ofstream( blocker ).put( 'x' );
112
113 wxString logPath = wxString::FromUTF8( ( blocker / "run.log" ).string().c_str() );
114 m_execute->SetConfiguredOutputPath( logPath );
115
117
118 BOOST_CHECK( !Run( reporter ) );
119 BOOST_CHECK_MESSAGE( reporter.GetMessages().Contains( logPath ), reporter.GetMessages() );
120}
121
122
123// Only Linux has a device that accepts the open and rejects every write
124#ifdef __linux__
125BOOST_AUTO_TEST_CASE( ExecuteCommandRecordOutputWriteFailureNamesLog )
126{
127 m_execute->SetConfiguredOutputPath( wxS( "/dev/full" ) );
128
129 // wx logs the expected ENOSPC itself, the reporter message is what the test checks
130 wxLogNull noLog;
131
133
134 BOOST_CHECK( !Run( reporter ) );
135 BOOST_CHECK_MESSAGE( reporter.GetMessages().Contains( wxS( "/dev/full" ) ), reporter.GetMessages() );
136}
137#endif
138
139
virtual void FromJson(const nlohmann::json &j)=0
A wrapper for reporting to a wxString object.
Definition reporter.h:242
std::shared_ptr< JOBS_OUTPUT_HANDLER > m_outputHandler
Definition jobset.h:137
std::vector< wxString > m_only
Transient property, not stored for now.
Definition jobset.h:138
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(ExecuteCommandRecordOutputIntoFolder)
IbisParser parser & reporter