示例#1
0
    def test_run(self) -> None:
        """
        Tests the method which let us run a command.
        """

        if PlatformUtility.is_unix():
            expected = ["Hello, World!"]
            actual = list(CommandHelper("echo 'Hello, World!'").run())

            self.assertEqual(expected, actual[:1])
示例#2
0
    def test_execute_empty_output(self) -> None:
        """
        Tests the method which executes a command for the case that the given
        command is empty.
        """

        if PlatformUtility.is_unix():
            expected = ""
            actual = self.helper.set_command("printf ''").execute()

            self.assertEqual(expected, actual)
示例#3
0
    def test_execute(self) -> None:
        """
        Tests the method which executes a command.
        """

        if PlatformUtility.is_unix():
            given = "echo 'Hello, World!'"
            expected = "Hello, World!\n"
            actual = self.helper.set_command(given).execute()

            self.assertEqual(expected, actual)
示例#4
0
def get_config_directory(
        *, project_name: str,
        project_version: str) -> str:  # pragma: no cover ## Not relevant
    """
    Provides the location of the configuration directory.
    """

    # pylint: disable=too-many-branches

    env_var_helper = EnvironmentVariableHelper()
    directory_helper = DirectoryHelper()

    if env_var_helper.set_name("PYFUNCEBLE_CONFIG_DIR").exists():
        config_directory = env_var_helper.get_value()
    elif env_var_helper.set_name("PYFUNCEBLE_OUTPUT_DIR").exists():
        config_directory = env_var_helper.get_value()
    elif (VersionUtility(project_version).is_cloned()
          or env_var_helper.set_name("TRAVIS_BUILD_DIR").exists()
          or env_var_helper.set_name("CI_PROJECT_DIR").exists()
          and env_var_helper.set_name("GITLAB_CI").exists()):
        config_directory = directory_helper.get_current(with_end_sep=True)
    else:
        if PlatformUtility.is_unix():
            config_dir_path = os.path.expanduser(os.path.join("~", ".config"))

            if directory_helper.set_path(config_dir_path).exists():
                config_directory = config_dir_path
            elif directory_helper.set_path(os.path.expanduser("~")).exists():
                config_directory = directory_helper.join_path(".")
            else:
                config_directory = directory_helper.get_current(
                    with_end_sep=True)
        elif PlatformUtility.is_windows():
            if env_var_helper.set_name("APPDATA").exists():
                config_directory = env_var_helper.get_value()
            else:
                config_directory = directory_helper.get_current(
                    with_end_sep=True)
        else:
            config_directory = directory_helper.get_current(with_end_sep=True)

        if not config_directory.endswith(os.sep):
            config_directory += os.sep
        config_directory += project_name + os.sep

        if not directory_helper.set_path(config_directory).exists():
            directory_helper.create()

    if not config_directory.endswith(os.sep):
        config_directory += os.sep

    return config_directory
示例#5
0
    def test_is_not_unix(self):
        """
        Tests the method which let us know if the current platform is the
        Linux one for that case that it's the Windows one.
        """

        with self.system_platform_mock as platform_patch:
            platform_patch.return_value = "Windows"

            expected = False
            actual = PlatformUtility.is_unix()

            self.assertEqual(expected, actual)
示例#6
0
    def test_is_unix_darwin(self):
        """
        Tests the method which let us know if the current platform is the
        Darwin (OSX) one.
        """

        with self.system_platform_mock as platform_patch:
            platform_patch.return_value = "Darwin"

            expected = True
            actual = PlatformUtility.is_unix()

            self.assertEqual(expected, actual)
示例#7
0
    def test_run_to_stdout(self) -> None:
        """
        Tests the method which let us run a command an output to stdout.
        """

        if PlatformUtility.is_unix():
            expected = "Hello, World!\n"

            CommandHelper("echo 'Hello, World!'").run_to_stdout()

            actual = sys.stdout.getvalue()

            self.assertEqual(expected, actual)
示例#8
0
    def test_execute_error(self, communicate_patch) -> None:
        """
        Tests the method which executes a command for the case that an
        error is produced by the command.
        """

        if PlatformUtility.is_unix():
            expected = "This is an error."

            communicate_patch.return_value = (b"", b"This is an error.")

            given = "echo 'This is an error.' 1>&2"
            actual = CommandHelper(given).execute()

            self.assertEqual(expected, actual)
示例#9
0
    def test_execute_error_exception(self, communicate_patch) -> None:
        """
        Tests the method which executes a command for the case that an
        error is produced by the command and the end-user want an exception when
        such cases happen.
        """

        if PlatformUtility.is_unix():
            communicate_patch.return_value = (b"", b"This is an error.")

            given = "echo 'This is an error.' 1>&2"
            self.helper.set_command(given)

            self.assertRaises(
                RuntimeError, lambda: self.helper.execute(raise_on_error=True)
            )
示例#10
0
    def generate_splitted_status_file(self) -> "StatusFileGenerator":
        """
        Generates the splitted status file.
        """

        self.file_printer.destination = os.path.join(
            self.get_output_basedir(),
            PyFunceble.cli.storage.OUTPUTS.splitted.directory,
            self.status.status.upper(),
        )

        if not PlatformUtility.is_unix():
            self.file_printer.destination += ".txt"

        self.file_printer.template_to_use = "all"
        self.file_printer.dataset = self.status.to_dict()
        self.file_printer.print_interpolated_line()

        return self
示例#11
0
    See the License for the specific language governing permissions and
    limitations under the License.
"""

import os
import sys
from typing import List, Optional

import colorama
from box import Box

import PyFunceble.cli.storage_facility
from PyFunceble.helpers.merge import Merge
from PyFunceble.utils.platform import PlatformUtility

if PlatformUtility.is_unix() and sys.stdin.encoding == "utf-8":
    STD_EPILOG: str = (
        f"Crafted with {colorama.Fore.RED}♥{colorama.Fore.RESET} by "
        f"{colorama.Style.BRIGHT}{colorama.Fore.CYAN}Nissar Chababy (@funilrys)"
        f"{colorama.Style.RESET_ALL} "
        f"with the help of\n{colorama.Style.BRIGHT}{colorama.Fore.GREEN}"
        f"https://git.io/JkUPS{colorama.Style.RESET_ALL} "
        f"&& {colorama.Style.BRIGHT}{colorama.Fore.GREEN}"
        f"https://git.io/JkUPF{colorama.Style.RESET_ALL}")

    ASCII_PYFUNCEBLE = """
    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
示例#12
0
        # to the producer queue.
        producer_process_manager.add_to_input_queue(
            (communication_dataset, test_result))

    # We are now done, it's time to send the stop signal.
    # The stop signal will inform thhe producer thread that it needs to stop
    # listening to new order (from the time it reads the stop signal).
    producer_process_manager.send_stop_signal(worker_name="main")

    # Now we wait until it's done.
    producer_process_manager.wait()

    # From here all files were generated we can do whatever we want with them.

    unix_path = os.path.join(dir_structure_restoration.get_output_basedir(),
                             "domains", "ACTIVE", "list")

    win_path = f"{unix_path}.txt"

    path_to_use = unix_path if PlatformUtility.is_unix() else win_path

    if os.path.isfile(path_to_use):
        print(f"{colorama.Style.BRIGHT}{colorama.Fore.GREEN}All right, "
              "files correctly generated!")
        sys.exit(0)
    else:
        print(
            f"{colorama.Style.BRIGHT}{colorama.Fore.RED}Something went wrong, "
            "files not correctly generated!")
        sys.exit(1)