def test_restart(self, serial_ports, serial, monkeypatch, mock_version_response): """Should pass the call through.""" restart = Mock() monkeypatch.setattr(NodeMCU, "restart", restart) assert main("--restart".split()) == 0 restart.assert_called_once_with()
def test_move(self, serial_ports, serial, monkeypatch, mock_version_response): """Should pass the call through.""" rename_file = Mock() monkeypatch.setattr(NodeMCU, "rename_file", rename_file) assert main("--rename foo.txt bar.txt".split()) == 0 rename_file.assert_called_once_with("foo.txt", "bar.txt")
def test_list_empty(self, serial_ports, serial, monkeypatch, mock_version_response, capsys): """Special case shouldn't break.""" monkeypatch.setattr(NodeMCU, "list_files", Mock(return_value={})) assert main("--list".split()) == 0 # NB: Aligned columns out, err = capsys.readouterr() assert out == "Total: 0 files, 0 bytes.\n"
def test_dofile(self, serial_ports, serial, monkeypatch, mock_version_response, capfd): """Dofile should run and the output printed.""" dofile = Mock(return_value=b"hello, there!\r\n") monkeypatch.setattr(NodeMCU, "dofile", dofile) assert main("--dofile foo.lua".split()) == 0 dofile.assert_called_once_with("foo.lua") out, err = capfd.readouterr() assert out == "hello, there!\r\n" # XXX: capfd always gives a string
def test_read(self, serial_ports, serial, monkeypatch, mock_version_response, capfd): """Reads should be passed through.""" read_file = Mock(return_value=b"foo") monkeypatch.setattr(NodeMCU, "read_file", read_file) assert main("--read foo.txt".split()) == 0 read_file.assert_called_once_with("foo.txt") out, err = capfd.readouterr() assert out == "foo" # XXX: capfd always gives a string...
def test_list_singletons(self, serial_ports, serial, monkeypatch, mock_version_response, capsys): """Grammar should be correct...""" files = { "a.txt": 1, } monkeypatch.setattr(NodeMCU, "list_files", Mock(return_value=files)) assert main("--list".split()) == 0 # NB: Aligned columns out, err = capsys.readouterr() assert out == "Total: 1 file, 1 byte.\na.txt 1\n"
def test_write(self, serial_ports, serial, monkeypatch, mock_version_response): """Writes should be passed through.""" import sys # Mock stdin stdin = Mock(read=Mock(return_value=b"foo")) stdin.return_value = stdin stdin.buffer = stdin monkeypatch.setattr(sys, "stdin", stdin) write_file = Mock() monkeypatch.setattr(NodeMCU, "write_file", write_file) assert main("--write foo.txt".split()) == 0 write_file.assert_called_once_with("foo.txt", b"foo")
def test_list(self, serial_ports, serial, monkeypatch, mock_version_response, capsys): """File listings should be formatted nicely.""" files = { "a.txt": 123, "bb.txt": 321, } monkeypatch.setattr(NodeMCU, "list_files", Mock(return_value=files)) assert main("--list".split()) == 0 # NB: Aligned columns out, err = capsys.readouterr() assert out in ( "Total: 2 files, 444 bytes.\na.txt 123\nbb.txt 321\n", "Total: 2 files, 444 bytes.\nbb.txt 321\na.txt 123\n", )
def test_no_ports_available(self, no_serial_ports, serial): """If no ports are available and none are specified, everything should fail.""" with pytest.raises(SystemExit): main("--list".split())
def test_manual_baudrate(self, serial_ports, serial, mock_format_response): """Baudrate should be overrideable.""" main("--baudrate 115200 --format".split()) serial.assert_called_once_with("/dev/ttyUSB5", 115200, timeout=2.0)
def test_bad_version(self, serial_ports, serial, monkeypatch, version): """Incompatible versions should fail.""" monkeypatch.setattr(NodeMCU, "get_version", Mock(return_value=version)) with pytest.raises(ValueError): main("--format".split())
def test_manual_port_overrides(self, serial_ports, serial, mock_format_response): """Specifying a port should override the default one.""" main("--port /dev/null --format".split()) serial.assert_called_once_with("/dev/null", 9600, timeout=2.0)
def test_sensible_port(self, serial_ports, serial, mock_format_response): """If several ports are available, select /dev/ttyUSB* by preference. """ main("--format".split()) serial.assert_called_once_with("/dev/ttyUSB5", 9600, timeout=2.0)
def test_manual_port(self, no_serial_ports, serial, mock_format_response): """If no ports are available specifying a port will fix things.""" main("--port /dev/null --format".split()) serial.assert_called_once_with("/dev/null", 9600, timeout=2.0)
def test_bad_arguments(self, args, serial_ports, serial): """Make sure various obvious bad arguments make the parser crash.""" with pytest.raises(SystemExit): main(args.split())