<commit_before>import re

from pygments.token import Token

import plenum.cli.cli as cli
from plenum.common.txn import TXN_TYPE, TARGET_NYM, DATA
from plenum.common.util import getMaxFailures
from plenum.test.eventually import eventually
from plenum.test.testable import Spyable
from plenum.test.helper import getAllArgs, checkSufficientRepliesRecvd, CREDIT, \
    AMOUNT, GET_BAL, GET_ALL_TXNS


class TestCliCore:
    @property
    def lastPrintArgs(self):
        args = self.printeds
        if args:
            return args[0]
        return None

    @property
    def lastPrintTokenArgs(self):
        args = self.printedTokens
        if args:
            return args[0]
        return None

    @property
    def printeds(self):
        return getAllArgs(self, TestCli.print)

    @property
    def printedTokens(self):
        return getAllArgs(self, TestCli.printTokens)

    def enterCmd(self, cmd: str):
        self.parse(cmd)


@Spyable(methods=[cli.Cli.print, cli.Cli.printTokens])
class TestCli(cli.Cli, TestCliCore):
    def initializeGrammar(self):
        fcTxnsGrams = [
            "(\s* (?P<client>client) \s+ (?P<client_name>[a-zA-Z0-9]+) \s+ (?P<cli_action>credit) \s+ (?P<amount>[0-9]+) \s+ to \s+(?P<second_client_name>[a-zA-Z0-9]+) \s*)  |",
            "(\s* (?P<client>client) \s+ (?P<client_name>[a-zA-Z0-9]+) \s+ (?P<cli_action>balance) \s*)  |",
            "(\s* (?P<client>client) \s+ (?P<client_name>[a-zA-Z0-9]+) \s+ (?P<cli_action>transactions) \s*)"
        ]
        self.clientGrams[-1] += " |"
        self.clientGrams += fcTxnsGrams
        cli.Cli.initializeGrammar(self)

    def _clientCommand(self, matchedVars):
        if matchedVars.get('client') == 'client':
            client_name = matchedVars.get('client_name')
            client_action = matchedVars.get('cli_action')
            if client_action == "credit":
                frm = client_name
                to = matchedVars.get('second_client_name')
                toClient = self.clients.get(to, None)
                amount = int(matchedVars.get('amount'))
                txn = {
                    TXN_TYPE: CREDIT,
                    TARGET_NYM: toClient.defaultIdentifier,
                    DATA: {
                        AMOUNT: amount
                    }}
                self.sendMsg(frm, txn)
                return True
            elif client_action == "balance":
                frm = client_name
                frmClient = self.clients.get(frm, None)
                txn = {
                    TXN_TYPE: GET_BAL,
                    TARGET_NYM: frmClient.defaultIdentifier
                }
                self.sendMsg(frm, txn)
                return True
            elif client_action == "transactions":
                frm = client_name
                frmClient = self.clients.get(frm, None)
                txn = {
                    TXN_TYPE: GET_ALL_TXNS,
                    TARGET_NYM: frmClient.defaultIdentifier
                }
                self.sendMsg(frm, txn)
                return True
            else:
                return cli.Cli._clientCommand(self, matchedVars)


def isErrorToken(token: Token):
    return token == Token.Error


def isHeadingToken(token: Token):
    return token == Token.Heading


def isNameToken(token: Token):
    return token == Token.Name


def checkNodeStarted(cli, nodeName):
    # Node name should be in cli.nodes
    assert nodeName in cli.nodes

    def chk():
        msgs = {stmt['msg'] for stmt in cli.printeds}
        assert "{} added replica {}:0 to instance 0 (master)" \
                   .format(nodeName, nodeName) in msgs
        assert "{} added replica {}:1 to instance 1 (backup)" \
                   .format(nodeName, nodeName) in msgs
        assert "{} listening for other nodes at {}:{}" \
                   .format(nodeName, *cli.nodes[nodeName].nodestack.ha) in msgs

    cli.looper.run(eventually(chk, retryWait=1, timeout=2))


def checkClientConnected(cli, nodeNames, clientName):
    printedMsgs = set()
    expectedMsgs = {'{} now connected to {}C'.format(clientName, nodeName)
                    for nodeName in nodeNames}
    for out in cli.printeds:
        msg = out.get('msg')
        if '{} now connected to'.format(clientName) in msg:
            printedMsgs.add(msg)

    assert printedMsgs == expectedMsgs


def checkRequest(cli, looper, operation):
    cName = "Joe"
    cli.enterCmd("new client {}".format(cName))
    # Let client connect to the nodes
    cli.looper.run(eventually(checkClientConnected, cli, list(cli.nodes.keys()),
                              cName, retryWait=1, timeout=5))
    # Send request to all nodes
    cli.enterCmd('client {} send {}'.format(cName, operation))
    client = cli.clients[cName]
    f = getMaxFailures(len(cli.nodes))
    # Ensure client gets back the replies
    looper.run(eventually(
            checkSufficientRepliesRecvd,
            client.inBox,
            client.lastReqId,
            f,
            retryWait=2,
            timeout=30))

    txn, status = client.getReply(client.lastReqId)

    # Ensure the cli shows appropriate output
    cli.enterCmd('client {} show {}'.format(cName, client.lastReqId))
    printeds = cli.printeds
    printedReply = printeds[1]
    printedStatus = printeds[0]
    txnTimePattern = "\'txnTime\', \d+\.*\d*"
    txnIdPattern = "\'txnId\', '" + txn['txnId'] + "'"
    assert re.search(txnIdPattern, printedReply['msg'])
    assert re.search(txnTimePattern, printedReply['msg'])
    assert printedStatus['msg'] == "Status: {}".format(status)<commit_msg>cli test helper bugfix<commit_after>import re

from pygments.token import Token

import plenum.cli.cli as cli
from plenum.common.txn import TXN_TYPE, TARGET_NYM, DATA
from plenum.common.util import getMaxFailures
from plenum.test.eventually import eventually
from plenum.test.testable import Spyable
from plenum.test.helper import getAllArgs, checkSufficientRepliesRecvd, CREDIT, \
    AMOUNT, GET_BAL, GET_ALL_TXNS


class TestCliCore:
    @property
    def lastPrintArgs(self):
        args = self.printeds
        if args:
            return args[0]
        return None

    @property
    def lastPrintTokenArgs(self):
        args = self.printedTokens
        if args:
            return args[0]
        return None

    @property
    def printeds(self):
        return getAllArgs(self, TestCli.print)

    @property
    def printedTokens(self):
        return getAllArgs(self, TestCli.printTokens)

    def enterCmd(self, cmd: str):
        self.parse(cmd)


@Spyable(methods=[cli.Cli.print, cli.Cli.printTokens])
class TestCli(cli.Cli, TestCliCore):
    def initializeGrammar(self):
        fcTxnsGrams = [
            "(\s* (?P<client>client) \s+ (?P<client_name>[a-zA-Z0-9]+) \s+ (?P<cli_action>credit) \s+ (?P<amount>[0-9]+) \s+ to \s+(?P<second_client_name>[a-zA-Z0-9]+) \s*)  |",
            "(\s* (?P<client>client) \s+ (?P<client_name>[a-zA-Z0-9]+) \s+ (?P<cli_action>balance) \s*)  |",
            "(\s* (?P<client>client) \s+ (?P<client_name>[a-zA-Z0-9]+) \s+ (?P<cli_action>transactions) \s*)"
        ]
        self.clientGrams[-1] += " |"
        self.clientGrams += fcTxnsGrams
        cli.Cli.initializeGrammar(self)

    def _clientCommand(self, matchedVars):
        if matchedVars.get('client') == 'client':
            client_name = matchedVars.get('client_name')
            client_action = matchedVars.get('cli_action')
            if client_action == "credit":
                frm = client_name
                to = matchedVars.get('second_client_name')
                toClient = self.clients.get(to, None)
                amount = int(matchedVars.get('amount'))
                txn = {
                    TXN_TYPE: CREDIT,
                    TARGET_NYM: toClient.defaultIdentifier,
                    DATA: {
                        AMOUNT: amount
                    }}
                self.sendMsg(frm, txn)
                return True
            elif client_action == "balance":
                frm = client_name
                frmClient = self.clients.get(frm, None)
                txn = {
                    TXN_TYPE: GET_BAL,
                    TARGET_NYM: frmClient.defaultIdentifier
                }
                self.sendMsg(frm, txn)
                return True
            elif client_action == "transactions":
                frm = client_name
                frmClient = self.clients.get(frm, None)
                txn = {
                    TXN_TYPE: GET_ALL_TXNS,
                    TARGET_NYM: frmClient.defaultIdentifier
                }
                self.sendMsg(frm, txn)
                return True
            else:
                return cli.Cli._clientCommand(self, matchedVars)


def isErrorToken(token: Token):
    return token == Token.Error


def isHeadingToken(token: Token):
    return token == Token.Heading


def isNameToken(token: Token):
    return token == Token.Name


def checkNodeStarted(cli, nodeName):
    # Node name should be in cli.nodes
    assert nodeName in cli.nodes

    def chk():
        msgs = {stmt['msg'] for stmt in cli.printeds}
        assert "{} added replica {}:0 to instance 0 (master)" \
                   .format(nodeName, nodeName) in msgs
        assert "{} added replica {}:1 to instance 1 (backup)" \
                   .format(nodeName, nodeName) in msgs
        assert "{} listening for other nodes at {}:{}" \
                   .format(nodeName, *cli.nodes[nodeName].nodestack.ha) in msgs

    cli.looper.run(eventually(chk, retryWait=1, timeout=2))


def checkClientConnected(cli, nodeNames, clientName):
    printedMsgs = set()
    expectedMsgs = {'{} now connected to {}C'.format(clientName, nodeName)
                    for nodeName in nodeNames}
    for out in cli.printeds:
        msg = out.get('msg')
        if '{} now connected to'.format(clientName) in msg:
            printedMsgs.add(msg)

    assert printedMsgs == expectedMsgs


def checkRequest(cli, looper, operation):
    cName = "Joe"
    cli.enterCmd("new client {}".format(cName))
    # Let client connect to the nodes
    cli.looper.run(eventually(checkClientConnected, cli, list(cli.nodes.keys()),
                              cName, retryWait=1, timeout=5))
    # Send request to all nodes
    cli.enterCmd('client {} send {}'.format(cName, operation))
    client = cli.clients[cName]
    f = getMaxFailures(len(cli.nodes))
    # Ensure client gets back the replies
    looper.run(eventually(
            checkSufficientRepliesRecvd,
            client.inBox,
            client.lastReqId,
            f,
            retryWait=2,
            timeout=30))

    txn, status = client.getReply(client.lastReqId)

    # Ensure the cli shows appropriate output
    cli.enterCmd('client {} show {}'.format(cName, client.lastReqId))
    printeds = cli.printeds
    printedReply = printeds[1]
    printedStatus = printeds[0]
    txnTimePattern = "\'txnTime\': \d+\.*\d*"
    txnIdPattern = "\'txnId\': '" + txn['txnId'] + "'"
    assert re.search(txnIdPattern, printedReply['msg'])
    assert re.search(txnTimePattern, printedReply['msg'])
    assert printedStatus['msg'] == "Status: {}".format(status)
{'fraction_non_alphanumeric': 0.11862736313950363, 'fraction_numerical': 0.004659080727533375, 'mean_word_length': 2.131874298540965, 'pattern_counts': {'":': 6, '<': 25, '<?xml version=': 0, '>': 25, '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': '1174310', 'n_tokens_mistral': 3611, 'n_tokens_neox': 3437, 'n_words': 758}