Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CommonIO testable #11589

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions libsolutil/CommonIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include <boost/filesystem.hpp>

#include <iostream>
#include <fstream>
#if defined(_WIN32)
#include <windows.h>
Expand Down Expand Up @@ -75,14 +74,14 @@ string solidity::util::readFileAsString(string const& _file)
return readFile<string>(_file);
}

string solidity::util::readStandardInput()
string solidity::util::readUntilEnd(istream& _stdin)
{
string ret;
while (!cin.eof())
while (!_stdin.eof())
{
string tmp;
// NOTE: this will read until EOF or NL
getline(cin, tmp);
getline(_stdin, tmp);
ret.append(tmp);
ret.append("\n");
}
Expand Down
9 changes: 5 additions & 4 deletions libsolutil/CommonIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@
#pragma once

#include <libsolutil/Common.h>
#include <iostream>
#include <sstream>
#include <string>

namespace solidity::util
{

/// Retrieve and returns the contents of the given file as a std::string.
/// Retrieves and returns the contents of the given file as a std::string.
/// If the file doesn't exist, it will throw a FileNotFound exception.
/// If the file exists but is not a regular file, it will throw NotAFile exception.
/// If the file is empty, returns an empty string.
std::string readFileAsString(std::string const& _file);

/// Retrieve and returns the contents of standard input (until EOF).
std::string readStandardInput();
/// Retrieves and returns the whole content of the specified input stream (until EOF).
std::string readUntilEnd(std::istream& _stdin);

/// Retrieve and returns a character from standard input (without waiting for EOL).
/// Retrieves and returns a character from standard input (without waiting for EOL).
int readStandardInputChar();

/// Converts arbitrary value to string representation using std::stringstream.
Expand Down
4 changes: 2 additions & 2 deletions solc/CommandLineInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ bool CommandLineInterface::readInputFilesAndConfigureRemappings()
}

if (addStdin)
m_fileReader.setSource(g_stdinFileName, readStandardInput());
m_fileReader.setSource(g_stdinFileName, readUntilEnd(cin));

if (m_fileReader.sourceCodes().size() == 0)
{
Expand Down Expand Up @@ -1255,7 +1255,7 @@ bool CommandLineInterface::processInput()
}
string input;
if (jsonFile.empty())
input = readStandardInput();
input = readUntilEnd(cin);
else
{
try
Expand Down
2 changes: 1 addition & 1 deletion test/tools/afl_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Allowed options)",
{
string input;
if (inputFile.size() == 0)
input = readStandardInput();
input = readUntilEnd(cin);
else
input = readFileAsString(inputFile);

Expand Down
2 changes: 1 addition & 1 deletion test/tools/yulrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Allowed options)",
}
}
else
input = readStandardInput();
input = readUntilEnd(cin);

interpret(input);
}
Expand Down