From a0430f76b74a11b44a865b28bb894a958e05f470 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 1 Aug 2019 23:10:20 +0200 Subject: [PATCH] [shellprocess] Apply current coding style --- src/modules/shellprocess/ShellProcessJob.cpp | 17 ++-- src/modules/shellprocess/ShellProcessJob.h | 2 +- src/modules/shellprocess/Tests.cpp | 92 ++++++++++---------- 3 files changed, 57 insertions(+), 54 deletions(-) diff --git a/src/modules/shellprocess/ShellProcessJob.cpp b/src/modules/shellprocess/ShellProcessJob.cpp index ba3e9a299..59d37d34b 100644 --- a/src/modules/shellprocess/ShellProcessJob.cpp +++ b/src/modules/shellprocess/ShellProcessJob.cpp @@ -18,13 +18,13 @@ #include "ShellProcessJob.h" -#include #include +#include #include #include "CalamaresVersion.h" -#include "JobQueue.h" #include "GlobalStorage.h" +#include "JobQueue.h" #include "utils/CommandList.h" #include "utils/Logger.h" @@ -55,7 +55,7 @@ Calamares::JobResult ShellProcessJob::exec() { - if ( ! m_commands || m_commands->isEmpty() ) + if ( !m_commands || m_commands->isEmpty() ) { cWarning() << "No commands to execute" << moduleInstanceKey(); return Calamares::JobResult::ok(); @@ -71,16 +71,23 @@ ShellProcessJob::setConfigurationMap( const QVariantMap& configurationMap ) bool dontChroot = CalamaresUtils::getBool( configurationMap, "dontChroot", false ); int timeout = CalamaresUtils::getInteger( configurationMap, "timeout", 10 ); if ( timeout < 1 ) + { timeout = 10; + } if ( configurationMap.contains( "script" ) ) { - m_commands = new CalamaresUtils::CommandList( configurationMap.value( "script" ), !dontChroot, std::chrono::seconds( timeout ) ); + m_commands = new CalamaresUtils::CommandList( + configurationMap.value( "script" ), !dontChroot, std::chrono::seconds( timeout ) ); if ( m_commands->isEmpty() ) + { cDebug() << "ShellProcessJob: \"script\" contains no commands for" << moduleInstanceKey(); + } } else + { cWarning() << "No script given for ShellProcessJob" << moduleInstanceKey(); + } } -CALAMARES_PLUGIN_FACTORY_DEFINITION( ShellProcessJobFactory, registerPlugin(); ) +CALAMARES_PLUGIN_FACTORY_DEFINITION( ShellProcessJobFactory, registerPlugin< ShellProcessJob >(); ) diff --git a/src/modules/shellprocess/ShellProcessJob.h b/src/modules/shellprocess/ShellProcessJob.h index 3111fc26e..b9a255d95 100644 --- a/src/modules/shellprocess/ShellProcessJob.h +++ b/src/modules/shellprocess/ShellProcessJob.h @@ -50,4 +50,4 @@ private: CALAMARES_PLUGIN_FACTORY_DECLARATION( ShellProcessJobFactory ) -#endif // SHELLPROCESSJOB_H +#endif // SHELLPROCESSJOB_H diff --git a/src/modules/shellprocess/Tests.cpp b/src/modules/shellprocess/Tests.cpp index 149fbc2d3..943a70957 100644 --- a/src/modules/shellprocess/Tests.cpp +++ b/src/modules/shellprocess/Tests.cpp @@ -37,13 +37,9 @@ using CommandList = CalamaresUtils::CommandList; using std::operator""s; -ShellProcessTests::ShellProcessTests() -{ -} +ShellProcessTests::ShellProcessTests() {} -ShellProcessTests::~ShellProcessTests() -{ -} +ShellProcessTests::~ShellProcessTests() {} void ShellProcessTests::initTestCase() @@ -66,16 +62,16 @@ ShellProcessTests::testProcessListSampleConfig() } } - CommandList cl( - CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); + CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); QVERIFY( !cl.isEmpty() ); QCOMPARE( cl.count(), 3 ); - QCOMPARE( cl.at(0).timeout(), CalamaresUtils::CommandLine::TimeoutNotSet() ); - QCOMPARE( cl.at(2).timeout(), 3600s ); // slowloris + QCOMPARE( cl.at( 0 ).timeout(), CalamaresUtils::CommandLine::TimeoutNotSet() ); + QCOMPARE( cl.at( 2 ).timeout(), 3600s ); // slowloris } -void ShellProcessTests::testProcessListFromList() +void +ShellProcessTests::testProcessListFromList() { YAML::Node doc = YAML::Load( R"(--- script: @@ -83,8 +79,7 @@ script: - "ls /nonexistent" - "/bin/false" )" ); - CommandList cl( - CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); + CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); QVERIFY( !cl.isEmpty() ); QCOMPARE( cl.count(), 3 ); @@ -95,52 +90,51 @@ script: - false - "ls /nonexistent" )" ); - CommandList cl1( - CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); + CommandList cl1( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); QVERIFY( !cl1.isEmpty() ); QCOMPARE( cl1.count(), 2 ); // One element ignored } -void ShellProcessTests::testProcessListFromString() +void +ShellProcessTests::testProcessListFromString() { YAML::Node doc = YAML::Load( R"(--- script: "ls /tmp" )" ); - CommandList cl( - CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); + CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); QVERIFY( !cl.isEmpty() ); QCOMPARE( cl.count(), 1 ); - QCOMPARE( cl.at(0).timeout(), 10s ); - QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) ); + QCOMPARE( cl.at( 0 ).timeout(), 10s ); + QCOMPARE( cl.at( 0 ).command(), QStringLiteral( "ls /tmp" ) ); // Not a string doc = YAML::Load( R"(--- script: false )" ); - CommandList cl1( - CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); + CommandList cl1( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); QVERIFY( cl1.isEmpty() ); QCOMPARE( cl1.count(), 0 ); } -void ShellProcessTests::testProcessFromObject() +void +ShellProcessTests::testProcessFromObject() { YAML::Node doc = YAML::Load( R"(--- script: command: "ls /tmp" timeout: 20 )" ); - CommandList cl( - CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); + CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); QVERIFY( !cl.isEmpty() ); QCOMPARE( cl.count(), 1 ); - QCOMPARE( cl.at(0).timeout(), 20s ); - QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) ); + QCOMPARE( cl.at( 0 ).timeout(), 20s ); + QCOMPARE( cl.at( 0 ).command(), QStringLiteral( "ls /tmp" ) ); } -void ShellProcessTests::testProcessListFromObject() +void +ShellProcessTests::testProcessListFromObject() { YAML::Node doc = YAML::Load( R"(--- script: @@ -148,34 +142,36 @@ script: timeout: 12 - "-/bin/false" )" ); - CommandList cl( - CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); + CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) ); QVERIFY( !cl.isEmpty() ); QCOMPARE( cl.count(), 2 ); - QCOMPARE( cl.at(0).timeout(), 12s ); - QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) ); - QCOMPARE( cl.at(1).timeout(), CalamaresUtils::CommandLine::TimeoutNotSet() ); // not set + QCOMPARE( cl.at( 0 ).timeout(), 12s ); + QCOMPARE( cl.at( 0 ).command(), QStringLiteral( "ls /tmp" ) ); + QCOMPARE( cl.at( 1 ).timeout(), CalamaresUtils::CommandLine::TimeoutNotSet() ); // not set } -void ShellProcessTests::testRootSubstitution() +void +ShellProcessTests::testRootSubstitution() { YAML::Node doc = YAML::Load( R"(--- script: - "ls /tmp" )" ); QVariant plainScript = CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ); - QVariant rootScript = CalamaresUtils::yamlMapToVariant( - YAML::Load( R"(--- + QVariant rootScript = CalamaresUtils::yamlMapToVariant( YAML::Load( R"(--- script: - "ls @@ROOT@@" -)" ) ).toMap().value( "script" ); - QVariant userScript = CalamaresUtils::yamlMapToVariant( - YAML::Load( R"(--- +)" ) ) + .toMap() + .value( "script" ); + QVariant userScript = CalamaresUtils::yamlMapToVariant( YAML::Load( R"(--- script: - mktemp -d @@ROOT@@/calatestXXXXXXXX - "chown @@USER@@ @@ROOT@@/calatest*" - rm -rf @@ROOT@@/calatest* -)" ) ).toMap().value( "script" ); +)" ) ) + .toMap() + .value( "script" ); if ( !Calamares::JobQueue::instance() ) (void)new Calamares::JobQueue( nullptr ); @@ -188,29 +184,29 @@ script: qDebug() << "Expect WARNING, ERROR, WARNING"; // Doesn't use @@ROOT@@, so no failures - QVERIFY( bool(CommandList(plainScript, false, 10s ).run()) ); + QVERIFY( bool( CommandList( plainScript, false, 10s ).run() ) ); // Doesn't use @@ROOT@@, but does chroot, so fails - QVERIFY( !bool(CommandList(plainScript, true, 10s ).run()) ); + QVERIFY( !bool( CommandList( plainScript, true, 10s ).run() ) ); // Does use @@ROOT@@, which is not set, so fails - QVERIFY( !bool(CommandList(rootScript, false, 10s ).run()) ); + QVERIFY( !bool( CommandList( rootScript, false, 10s ).run() ) ); // .. fails for two reasons - QVERIFY( !bool(CommandList(rootScript, true, 10s ).run()) ); + QVERIFY( !bool( CommandList( rootScript, true, 10s ).run() ) ); gs->insert( "rootMountPoint", "/tmp" ); // Now that the root is set, two variants work .. still can't // chroot, unless the rootMountPoint contains a full system, // *and* we're allowed to chroot (ie. running tests as root). qDebug() << "Expect no output."; - QVERIFY( bool(CommandList(plainScript, false, 10s ).run()) ); - QVERIFY( bool(CommandList(rootScript, false, 10s ).run()) ); + QVERIFY( bool( CommandList( plainScript, false, 10s ).run() ) ); + QVERIFY( bool( CommandList( rootScript, false, 10s ).run() ) ); qDebug() << "Expect ERROR"; // But no user set yet - QVERIFY( !bool(CommandList(userScript, false, 10s ).run()) ); + QVERIFY( !bool( CommandList( userScript, false, 10s ).run() ) ); // Now play dangerous games with shell expansion gs->insert( "username", "`id -u`" ); - QVERIFY( bool(CommandList(userScript, false, 10s ).run()) ); + QVERIFY( bool( CommandList( userScript, false, 10s ).run() ) ); }