Skip to content

Commit

Permalink
Add tool "diff" for Linux and Unix (#3501)
Browse files Browse the repository at this point in the history
* Update __init__.py

* Add files via upload

* Update diff.py
  • Loading branch information
shekharsorot authored Nov 11, 2024
1 parent b3e1f46 commit 681f5bd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lisa/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .date import Date
from .df import Df
from .dhclient import Dhclient
from .diff import Diff
from .dmesg import Dmesg
from .dnsmasq import Dnsmasq
from .docker import Docker
Expand Down Expand Up @@ -140,6 +141,7 @@
"Date",
"Df",
"Dhclient",
"Diff",
"Dmesg",
"Dnsmasq",
"Docker",
Expand Down
70 changes: 70 additions & 0 deletions lisa/tools/diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
from pathlib import PurePath
from typing import Optional

from lisa.executable import Tool


class Diff(Tool):
"""
Diff is a tool class for comparing files or directories using the 'diff' command.
Attributes:
command (str): The command to be executed, which is 'diff'.
can_install (bool): Indicates whether the tool can be installed.
Always False for this tool.
Methods:
_check_exists() -> bool:
Checks if the tool exists. Always returns True for this tool.
comparefiles(
timeout: int = 600
Compares two files or directories.
Args:
src (PurePath): The source file or directory.
dest (PurePath): The destination file or directory.
cwd (Optional[PurePath]): The current working directory for the command.
Defaults to None.
sudo (bool): Whether to run the command with sudo. Defaults to False.
timeout (int): The timeout for the command in seconds. Defaults to 600.
Returns:
str: The output of the diff command.
Raises:
AssertionError: If the exit code of the diff command is not 0 or 1.
"""

@property
def command(self) -> str:
return "diff"

@property
def can_install(self) -> bool:
return False

def comparefiles(
self,
src: PurePath,
dest: PurePath,
cwd: Optional[PurePath] = None,
sudo: bool = False,
timeout: int = 600,
) -> str:
cmd = f"{self.node.get_str_path(src)} {self.node.get_str_path(dest)}"
result = self.run(
cmd,
force_run=True,
sudo=sudo,
cwd=cwd,
shell=True,
timeout=timeout,
)
# Diff generated difference between FILE1 FILE2 or DIR1 DIR2 or DIR FILE
# for FILE DIR
# EXit status is 0 if inputs are the same. 1 if different, 2 if trouble
result.assert_exit_code([0, 1], f"Diff Error: {result.stderr}")
return result.stdout

0 comments on commit 681f5bd

Please sign in to comment.