Navigation Menu

Skip to content

Commit

Permalink
Merge #13054: tests: Enable automatic detection of undefined names in…
Browse files Browse the repository at this point in the history
… Python tests scripts. Remove wildcard imports.

68400d8 tests: Use explicit imports (practicalswift)

Pull request description:

  Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.

  Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.

  An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.

  Before this commit:

  ```
  $ contrib/devtools/lint-python.sh | head -10
  ./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
  ./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
  ./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
  ./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
  ./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
  ./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
  ./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
  ./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
  ./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
  ./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
  $
  ```

  After this commit:

  ```
  $ contrib/devtools/lint-python.sh | head -10
  $
  ```

Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
  • Loading branch information
MarcoFalke committed Aug 13, 2018
2 parents 73a09b4 + 68400d8 commit bffb35f
Show file tree
Hide file tree
Showing 50 changed files with 143 additions and 94 deletions.
8 changes: 6 additions & 2 deletions test/functional/feature_bip68_sequence.py
Expand Up @@ -4,9 +4,13 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test BIP68 implementation."""

import time

from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, ToHex
from test_framework.script import CScript
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.blocktools import *
from test_framework.util import assert_equal, assert_greater_than, assert_raises_rpc_error, bytes_to_hex_str, get_bip9_status, satoshi_round, sync_blocks

SEQUENCE_LOCKTIME_DISABLE_FLAG = (1<<31)
SEQUENCE_LOCKTIME_TYPE_FLAG = (1<<22) # this means use time (0 means height)
Expand Down
8 changes: 5 additions & 3 deletions test/functional/feature_cltv.py
Expand Up @@ -8,11 +8,13 @@
1351.
"""

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.mininode import *
from test_framework.blocktools import create_coinbase, create_block, create_transaction
from test_framework.messages import CTransaction, msg_block, ToHex
from test_framework.mininode import mininode_lock, P2PInterface
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP, CScriptNum
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, bytes_to_hex_str, hex_str_to_bytes, wait_until

from io import BytesIO

CLTV_HEIGHT = 1351
Expand Down
5 changes: 2 additions & 3 deletions test/functional/feature_dbcrash.py
Expand Up @@ -31,10 +31,9 @@
import sys
import time

from test_framework.mininode import *
from test_framework.script import *
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, ToHex
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.util import assert_equal, create_confirmed_utxos, hex_str_to_bytes

HTTP_DISCONNECT_ERRORS = [http.client.CannotSendRequest]
try:
Expand Down
7 changes: 4 additions & 3 deletions test/functional/feature_dersig.py
Expand Up @@ -7,11 +7,12 @@
Test that the DERSIG soft-fork activates at (regtest) height 1251.
"""

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.mininode import *
from test_framework.blocktools import create_coinbase, create_block, create_transaction
from test_framework.messages import msg_block
from test_framework.mininode import mininode_lock, P2PInterface
from test_framework.script import CScript
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, bytes_to_hex_str, wait_until

DERSIG_HEIGHT = 1251

Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_fee_estimation.py
Expand Up @@ -6,7 +6,7 @@
from decimal import Decimal
import random

from test_framework.mininode import CTransaction, CTxIn, CTxOut, COutPoint, ToHex, COIN
from test_framework.messages import CTransaction, CTxIn, CTxOut, COutPoint, ToHex, COIN
from test_framework.script import CScript, OP_1, OP_DROP, OP_2, OP_HASH160, OP_EQUAL, hash160, OP_TRUE
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
Expand Down
5 changes: 3 additions & 2 deletions test/functional/feature_maxuploadtarget.py
Expand Up @@ -13,9 +13,10 @@
from collections import defaultdict
import time

from test_framework.mininode import *
from test_framework.messages import CInv, msg_getdata
from test_framework.mininode import P2PInterface
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.util import assert_equal, mine_large_block

class TestP2PConn(P2PInterface):
def __init__(self):
Expand Down
7 changes: 4 additions & 3 deletions test/functional/feature_nulldummy.py
Expand Up @@ -13,11 +13,12 @@
[Policy/Consensus] Check that the new NULLDUMMY rules are enforced on the 432nd block.
"""

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.messages import CTransaction
from test_framework.blocktools import create_coinbase, create_block, create_transaction, add_witness_commitment
from test_framework.messages import CTransaction
from test_framework.script import CScript
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error, bytes_to_hex_str

import time

NULLDUMMY_ERROR = "non-mandatory-script-verify-flag (Dummy CHECKMULTISIG argument must be zero) (code 64)"
Expand Down
3 changes: 2 additions & 1 deletion test/functional/feature_pruning.py
Expand Up @@ -10,7 +10,8 @@
"""

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.util import assert_equal, assert_greater_than, assert_raises_rpc_error, connect_nodes, mine_large_block, sync_blocks, wait_until

import os

MIN_BLOCKS_TO_KEEP = 288
Expand Down
8 changes: 5 additions & 3 deletions test/functional/feature_rbf.py
Expand Up @@ -4,10 +4,12 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the RBF code."""

from decimal import Decimal

from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut
from test_framework.script import CScript, OP_DROP
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.script import *
from test_framework.mininode import *
from test_framework.util import assert_equal, assert_raises_rpc_error, bytes_to_hex_str, satoshi_round

MAX_REPLACEMENT_LIMIT = 100

Expand Down
12 changes: 8 additions & 4 deletions test/functional/feature_segwit.py
Expand Up @@ -4,19 +4,23 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the SegWit changeover logic."""

from decimal import Decimal

from test_framework.address import (
key_to_p2pkh,
key_to_p2sh_p2wpkh,
key_to_p2wpkh,
program_to_witness,
script_to_p2sh,
script_to_p2sh_p2wsh,
script_to_p2wsh,
)
from test_framework.blocktools import witness_script, send_to_witness
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.mininode import sha256, CTransaction, CTxIn, COutPoint, CTxOut, COIN, ToHex, FromHex
from test_framework.address import script_to_p2sh, key_to_p2pkh
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, sha256, ToHex
from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, OP_TRUE, OP_DROP
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error, bytes_to_hex_str, connect_nodes, hex_str_to_bytes, sync_blocks, try_rpc

from io import BytesIO

NODE_0 = 0
Expand Down
2 changes: 1 addition & 1 deletion test/functional/interface_http.py
Expand Up @@ -5,7 +5,7 @@
"""Test the RPC HTTP basics."""

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.util import assert_equal, str_to_b64str

import http.client
import urllib.parse
Expand Down
2 changes: 1 addition & 1 deletion test/functional/interface_zmq.py
Expand Up @@ -7,7 +7,7 @@

from test_framework.test_framework import (
BitcoinTestFramework, skip_if_no_bitcoind_zmq, skip_if_no_py3_zmq)
from test_framework.mininode import CTransaction
from test_framework.messages import CTransaction
from test_framework.util import (assert_equal,
bytes_to_hex_str,
hash256,
Expand Down
4 changes: 3 additions & 1 deletion test/functional/mempool_limit.py
Expand Up @@ -4,8 +4,10 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test mempool limiting together/eviction with the wallet."""

from decimal import Decimal

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.util import assert_equal, assert_greater_than, assert_raises_rpc_error, create_confirmed_utxos, create_lots_of_big_transactions, gen_return_txouts

class MempoolLimitTest(BitcoinTestFramework):
def set_test_params(self):
Expand Down
6 changes: 4 additions & 2 deletions test/functional/mempool_packages.py
Expand Up @@ -4,9 +4,11 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test descendant package tracking code."""

from decimal import Decimal

from test_framework.messages import COIN
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.mininode import COIN
from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round, sync_blocks, sync_mempools

MAX_ANCESTORS = 25
MAX_DESCENDANTS = 25
Expand Down
3 changes: 2 additions & 1 deletion test/functional/mempool_persist.py
Expand Up @@ -35,11 +35,12 @@
node1 can't write to disk.
"""
from decimal import Decimal
import os
import time

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.util import assert_equal, assert_raises_rpc_error, wait_until

class MempoolPersistTest(BitcoinTestFramework):
def set_test_params(self):
Expand Down
4 changes: 2 additions & 2 deletions test/functional/mempool_reorg.py
Expand Up @@ -8,9 +8,9 @@
that spend (directly or indirectly) coinbase transactions.
"""

from test_framework.test_framework import BitcoinTestFramework
from test_framework.blocktools import create_raw_transaction
from test_framework.util import *
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error


class MempoolCoinbaseTest(BitcoinTestFramework):
Expand Down
4 changes: 2 additions & 2 deletions test/functional/mempool_resurrect.py
Expand Up @@ -4,9 +4,9 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test resurrection of mined transactions when the blockchain is re-organized."""

from test_framework.test_framework import BitcoinTestFramework
from test_framework.blocktools import create_raw_transaction
from test_framework.util import *
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal


class MempoolCoinbaseTest(BitcoinTestFramework):
Expand Down
2 changes: 1 addition & 1 deletion test/functional/mempool_spend_coinbase.py
Expand Up @@ -14,7 +14,7 @@

from test_framework.test_framework import BitcoinTestFramework
from test_framework.blocktools import create_raw_transaction
from test_framework.util import *
from test_framework.util import assert_equal, assert_raises_rpc_error


class MempoolSpendCoinbaseTest(BitcoinTestFramework):
Expand Down
2 changes: 1 addition & 1 deletion test/functional/mining_basic.py
Expand Up @@ -13,7 +13,7 @@
from decimal import Decimal

from test_framework.blocktools import create_coinbase
from test_framework.mininode import CBlock
from test_framework.messages import CBlock
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error

Expand Down
4 changes: 3 additions & 1 deletion test/functional/mining_getblocktemplate_longpoll.py
Expand Up @@ -4,8 +4,10 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test longpolling with getblocktemplate."""

from decimal import Decimal

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.util import get_rpc_proxy, random_transaction

import threading

Expand Down
6 changes: 4 additions & 2 deletions test/functional/mining_prioritisetransaction.py
Expand Up @@ -4,9 +4,11 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the prioritisetransaction mining RPC."""

import time

from test_framework.messages import COIN, MAX_BLOCK_BASE_SIZE
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.mininode import COIN, MAX_BLOCK_BASE_SIZE
from test_framework.util import assert_equal, assert_raises_rpc_error, create_confirmed_utxos, create_lots_of_big_transactions, gen_return_txouts

class PrioritiseTransactionTest(BitcoinTestFramework):
def set_test_params(self):
Expand Down
11 changes: 7 additions & 4 deletions test/functional/p2p_compactblocks.py
Expand Up @@ -8,12 +8,15 @@
Version 2 compact blocks are post-segwit (wtxids)
"""

from test_framework.mininode import *
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from decimal import Decimal
import random

from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment
from test_framework.messages import BlockTransactions, BlockTransactionsRequest, calculate_shortid, CBlock, CBlockHeader, CInv, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, HeaderAndShortIDs, msg_block, msg_blocktxn, msg_cmpctblock, msg_getblocktxn, msg_getdata, msg_getheaders, msg_headers, msg_inv, msg_sendcmpct, msg_sendheaders, msg_tx, msg_witness_block, msg_witness_blocktxn, MSG_WITNESS_FLAG, NODE_NETWORK, NODE_WITNESS, P2PHeaderAndShortIDs, PrefilledTransaction, ser_uint256, ToHex
from test_framework.mininode import mininode_lock, P2PInterface
from test_framework.script import CScript, OP_TRUE, OP_DROP

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, get_bip9_status, satoshi_round, sync_blocks, wait_until

# TestP2PConn: A peer we use to send messages to bitcoind, and store responses.
class TestP2PConn(P2PInterface):
Expand Down
8 changes: 5 additions & 3 deletions test/functional/p2p_feefilter.py
Expand Up @@ -4,11 +4,13 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test processing of feefilter messages."""

from test_framework.mininode import *
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from decimal import Decimal
import time

from test_framework.messages import msg_feefilter
from test_framework.mininode import mininode_lock, P2PInterface
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import sync_blocks, sync_mempools

def hashToHex(hash):
return format(hash, '064x')
Expand Down
4 changes: 2 additions & 2 deletions test/functional/p2p_fingerprint.py
Expand Up @@ -11,18 +11,18 @@
import time

from test_framework.blocktools import (create_block, create_coinbase)
from test_framework.messages import CInv
from test_framework.mininode import (
CInv,
P2PInterface,
msg_headers,
msg_block,
msg_getdata,
msg_getheaders,
wait_until,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
wait_until,
)

class P2PFingerprintTest(BitcoinTestFramework):
Expand Down
7 changes: 5 additions & 2 deletions test/functional/p2p_leak.py
Expand Up @@ -10,9 +10,12 @@
This test connects to a node and sends it a few messages, trying to entice it
into sending us something it shouldn't."""

from test_framework.mininode import *
import time

from test_framework.messages import msg_getaddr, msg_ping, msg_verack
from test_framework.mininode import mininode_lock, P2PInterface
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.util import wait_until

banscore = 10

Expand Down
5 changes: 3 additions & 2 deletions test/functional/p2p_mempool.py
Expand Up @@ -8,9 +8,10 @@
filters are not enabled.
"""

from test_framework.mininode import *
from test_framework.messages import msg_mempool
from test_framework.mininode import P2PInterface
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
from test_framework.util import assert_equal

class P2PMempoolTests(BitcoinTestFramework):
def set_test_params(self):
Expand Down
6 changes: 3 additions & 3 deletions test/functional/p2p_node_network_limited.py
Expand Up @@ -8,10 +8,10 @@
and that it responds to getdata requests for blocks correctly:
- send a block within 288 + 2 of the tip
- disconnect peers who request blocks older than that."""
from test_framework.messages import CInv, msg_getdata, msg_verack
from test_framework.mininode import NODE_BLOOM, NODE_NETWORK_LIMITED, NODE_WITNESS, P2PInterface, wait_until, mininode_lock
from test_framework.messages import CInv, msg_getdata, msg_verack, NODE_BLOOM, NODE_NETWORK_LIMITED, NODE_WITNESS
from test_framework.mininode import P2PInterface, mininode_lock
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, disconnect_nodes, connect_nodes_bi, sync_blocks
from test_framework.util import assert_equal, disconnect_nodes, connect_nodes_bi, sync_blocks, wait_until

class P2PIgnoreInv(P2PInterface):
firstAddrnServices = 0
Expand Down
2 changes: 1 addition & 1 deletion test/functional/p2p_sendheaders.py
Expand Up @@ -86,9 +86,9 @@
Expect: disconnect.
"""
from test_framework.blocktools import create_block, create_coinbase
from test_framework.messages import CInv
from test_framework.mininode import (
CBlockHeader,
CInv,
NODE_WITNESS,
P2PInterface,
mininode_lock,
Expand Down

0 comments on commit bffb35f

Please sign in to comment.