Skip to content

Commit

Permalink
Merge pull request #11589 from ethereum/testable-common-io
Browse files Browse the repository at this point in the history
Make CommonIO testable
  • Loading branch information
cameel authored Jul 2, 2021
2 parents 98e1dee + a72857d commit f6cb933
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
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

0 comments on commit f6cb933

Please sign in to comment.