[libcalamares] Expand Runner documentation

- document a bit more of the methods
- provide convenience method enableOutputProcessing() alongside
  an explicit setter; adjust tests to the changed API.
- add an executable() information method.
This commit is contained in:
Adriaan de Groot 2021-11-03 12:20:36 +01:00
parent f2142bc4b3
commit e5323ec487
2 changed files with 23 additions and 6 deletions

View File

@ -32,10 +32,15 @@ using ProcessResult = CalamaresUtils::ProcessResult;
/** @brief A Runner wraps a process and handles running it and processing output /** @brief A Runner wraps a process and handles running it and processing output
* *
* This is basically a QProcess, but handles both running in the * This is basically a QProcess, but handles both running in the
* host system (natively) or in the target (by calling chroot). * host system (through env(1)) or in the target (by calling chroot(8)).
* It has an output signal that handles output one line at a time * It has an output signal that handles output one line at a time
* (unlike QProcess that lets you do the buffering yourself). * (unlike QProcess that lets you do the buffering yourself).
* This output processing is only enabled if you do so explicitly. * This output processing is only enabled if you do so explicitly.
*
* Use the set*() methods to configure the runner.
*
* If you call enableOutputProcessing(), then you can connect to
* the output() signal to receive each line (including trailing newline!).
*/ */
class Runner : public QObject class Runner : public QObject
{ {
@ -86,14 +91,26 @@ public:
m_input = stdin; m_input = stdin;
return *this; return *this;
} }
Runner& setOutputProcessing( bool enable )
Runner& enableOutputProcessing( bool enable = true )
{ {
m_output = enable; m_output = enable;
return *this; return *this;
} }
Runner& enableOutputProcessing()
{
m_output = true;
return *this;
}
ProcessResult run(); ProcessResult run();
/** @brief The executable (argv[0]) that this runner will run
*
* This is the first element of the command; it does not include
* env(1) or chroot(8) which are injected when actually running
* the command.
*/
QString executable() const { return m_command.isEmpty() ? QString() : m_command.first(); }
signals: signals:
void output( QString line ); void output( QString line );

View File

@ -892,7 +892,7 @@ LibCalamaresTests::testRunnerOutput()
{ {
Calamares::Utils::Runner r( { "ls", "-d", "." } ); Calamares::Utils::Runner r( { "ls", "-d", "." } );
QSignalSpy spy( &r, &decltype( r )::output ); QSignalSpy spy( &r, &decltype( r )::output );
r.enableOutputProcessing( true ); r.enableOutputProcessing();
auto result = r.run(); auto result = r.run();
QCOMPARE( result.getExitCode(), 0 ); QCOMPARE( result.getExitCode(), 0 );
@ -904,7 +904,7 @@ LibCalamaresTests::testRunnerOutput()
{ {
Calamares::Utils::Runner r( { "cat" } ); Calamares::Utils::Runner r( { "cat" } );
QSignalSpy spy( &r, &decltype( r )::output ); QSignalSpy spy( &r, &decltype( r )::output );
r.enableOutputProcessing( true ).setInput( QStringLiteral( "hello\nworld\n\n!\n" ) ); r.enableOutputProcessing().setInput( QStringLiteral( "hello\nworld\n\n!\n" ) );
{ {
auto result = r.run(); auto result = r.run();
@ -927,7 +927,7 @@ LibCalamaresTests::testRunnerOutput()
QStringList collectedOutput; QStringList collectedOutput;
Calamares::Utils::Runner r( { "cat" } ); Calamares::Utils::Runner r( { "cat" } );
r.enableOutputProcessing( true ).setInput( QStringLiteral( "hello\nworld\n\n!\n" ) ); r.enableOutputProcessing().setInput( QStringLiteral( "hello\nworld\n\n!\n" ) );
QObject::connect( &r, &decltype( r )::output, [&collectedOutput]( QString s ) { collectedOutput << s; } ); QObject::connect( &r, &decltype( r )::output, [&collectedOutput]( QString s ) { collectedOutput << s; } );
{ {