[libcalamares] Truncate strings without trailing newline properly

This commit is contained in:
Adriaan de Groot 2021-01-28 15:30:00 +01:00
parent 9b15df595e
commit 1542bad224
2 changed files with 27 additions and 2 deletions

View File

@ -135,7 +135,8 @@ truncateMultiLine( const QString& string, CalamaresUtils::LinesStartEnd lines, C
return shorter;
}
if ( ( string.length() <= chars.total ) && ( string.count( NEWLINE ) <= maxLines ) )
const int linesInString = string.count( NEWLINE ) + ( string.endsWith( NEWLINE ) ? 0 : 1 );
if ( ( string.length() <= chars.total ) && ( linesInString <= maxLines ) )
{
return string;
}

View File

@ -600,13 +600,37 @@ and the translations updated.)" );
// Grab first line, untruncated
{
auto s = truncateMultiLine( longString, LinesStartEnd { 1, 0 } );
QVERIFY( s.length() > 1 );
QVERIFY( longString.startsWith( s ) );
QVERIFY( s.endsWith( NEWLINE ) );
QVERIFY( s.endsWith( "being\n" ) );
QVERIFY( s.startsWith( "Some " ) );
}
// Grab last line, untruncated
{
auto s = truncateMultiLine( longString, LinesStartEnd { 0, 1 } );
QVERIFY( s.length() > 1 );
QVERIFY( longString.endsWith( s ) );
QVERIFY( !s.endsWith( NEWLINE ) );
QVERIFY( s.endsWith( "updated." ) );
QCOMPARE( s.count( NEWLINE ), 0 ); // Because last line doesn't end with a newline
QVERIFY( s.startsWith( "and the " ) );
}
// Grab last two lines, untruncated
{
auto s = truncateMultiLine( longString, LinesStartEnd { 0, 2 } );
QVERIFY( s.length() > 1 );
QVERIFY( longString.endsWith( s ) );
QVERIFY( !s.endsWith( NEWLINE ) );
QVERIFY( s.endsWith( "updated." ) );
cDebug() << "Result-line" << Logger::Quote << s;
QCOMPARE( s.count( NEWLINE ), 1 ); // Because last line doesn't end with a newline
QVERIFY( s.startsWith( "displayed in " ) );
}
}