<commit_before>#ifndef TEST_HELPERS_H_
#define TEST_HELPERS_H_

#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <string>

namespace TestHelpers {

	/* Objects of TempFile class generate a temporary filename and delete the
	 * corresponding file when they are destructed.
	 *
	 * This is useful for teardown in tests, where we use RAII to clean up
	 * temporary DB files and stuff. */
	class TempFile {
		public:
			class tempfileexception : public std::exception {
				public:
					explicit tempfileexception(const char* error) {
						msg  = "failed to create a tempdir: ";
						msg += error;
					};

					virtual const char* what() const throw() {
						return msg.c_str();
					}

				private:
					std::string msg;
			};

			TempFile() {
				init_tempdir();
				init_file();
			}

			~TempFile() {
				::unlink(filepath.c_str());
				::rmdir(tempdir.c_str());
			}

			const std::string getPath() {
				return filepath;
			}

		private:
			void init_tempdir() {
				char* tmpdir_p = ::getenv("TMPDIR");

				if (tmpdir_p) {
					tempdir = tmpdir_p;
				} else {
					tempdir = "/tmp/";
				}

				tempdir += "/newsbeuter-tests/";

				int status = mkdir(tempdir.c_str(), S_IRWXU);
				if (status != 0) {
					// The directory already exists. That's fine, though, but
					// only as long as it has all the properties we need.

					int saved_errno = errno;
					bool success = false;

					if (saved_errno == EEXIST) {
						struct stat buffer;
						if (lstat(filepath.c_str(), &buffer) == 0) {
							if (   buffer.st_mode & S_IRUSR
								&& buffer.st_mode & S_IWUSR<commit_msg>Fix the typo in the previous commit<commit_after>#ifndef TEST_HELPERS_H_
#define TEST_HELPERS_H_

#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <sys/stat.h>
#include <string>

namespace TestHelpers {

	/* Objects of TempFile class generate a temporary filename and delete the
	 * corresponding file when they are destructed.
	 *
	 * This is useful for teardown in tests, where we use RAII to clean up
	 * temporary DB files and stuff. */
	class TempFile {
		public:
			class tempfileexception : public std::exception {
				public:
					explicit tempfileexception(const char* error) {
						msg  = "failed to create a tempdir: ";
						msg += error;
					};

					virtual const char* what() const throw() {
						return msg.c_str();
					}

				private:
					std::string msg;
			};

			TempFile() {
				init_tempdir();
				init_file();
			}

			~TempFile() {
				::unlink(filepath.c_str());
				::rmdir(tempdir.c_str());
			}

			const std::string getPath() {
				return filepath;
			}

		private:
			void init_tempdir() {
				char* tmpdir_p = ::getenv("TMPDIR");

				if (tmpdir_p) {
					tempdir = tmpdir_p;
				} else {
					tempdir = "/tmp/";
				}

				tempdir += "/newsbeuter-tests/";

				int status = mkdir(tempdir.c_str(), S_IRWXU);
				if (status != 0) {
					// The directory already exists. That's fine, though, but
					// only as long as it has all the properties we need.

					int saved_errno = errno;
					bool success = false;

					if (saved_errno == EEXIST) {
						struct stat buffer;
						if (lstat(tempdir.c_str(), &buffer) == 0) {
							if (   buffer.st_mode & S_IRUSR
								&& buffer.st_mode & S_IWUSR
{'fraction_non_alphanumeric': 0.12015858493443123, 'fraction_numerical': 0.001219884111009454, 'mean_word_length': 2.6403995560488345, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4583960', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1085, 'n_words': 317}