Skip to content

Commit

Permalink
update tests to include the newline added to txt files by precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Aug 29, 2024
1 parent ffc62f8 commit f0f27de
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 74 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,10 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# pixi environments
.pixi
*.egg-info

# magic environments
.magic
8 changes: 8 additions & 0 deletions magic.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 5
environments:
default:
channels:
- url: https://conda.anaconda.org/conda-forge/
- url: https://conda.modular.com/max-nightly/
packages: {}
packages: []
14 changes: 14 additions & 0 deletions mojoproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[project]
authors = ["Mikhail Tavarez <[email protected]>"]
channels = ["conda-forge", "https://conda.modular.com/max-nightly"]
description = "Add a short description here"
name = "gojo"
platforms = ["osx-arm64"]
version = "0.1.0"

[tasks]
test = "bash scripts/run_tests.sh"
prepare-tests = "bash scripts/build.sh tests"
package = "bash scripts/build.sh package"

[dependencies]
42 changes: 40 additions & 2 deletions src/gojo/syscall/net.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ struct sockaddr_in6:


@value
@register_passable("trivial")
struct addrinfo:
"""Struct field ordering can vary based on platform.
For MacOS, I had to swap the order of ai_canonname and ai_addr.
Expand All @@ -358,9 +357,28 @@ struct addrinfo:
var ai_addr: UnsafePointer[sockaddr]
var ai_next: UnsafePointer[addrinfo]

fn __init__(
inout self,
ai_flags: c_int = 0,
ai_family: c_int = 0,
ai_socktype: c_int = 0,
ai_protocol: c_int = 0,
ai_addrlen: socklen_t = 0,
ai_canonname: UnsafePointer[UInt8] = UnsafePointer[UInt8](),
ai_addr: UnsafePointer[sockaddr] = UnsafePointer[sockaddr](),
ai_next: UnsafePointer[addrinfo] = UnsafePointer[addrinfo](),
):
self.ai_flags = ai_flags
self.ai_family = ai_family
self.ai_socktype = ai_socktype
self.ai_protocol = ai_protocol
self.ai_addrlen = ai_addrlen
self.ai_canonname = ai_canonname
self.ai_addr = ai_addr
self.ai_next = ai_next


@value
@register_passable("trivial")
struct addrinfo_unix:
"""Struct field ordering can vary based on platform.
For MacOS, I had to swap the order of ai_canonname and ai_addr.
Expand All @@ -376,6 +394,26 @@ struct addrinfo_unix:
var ai_canonname: UnsafePointer[UInt8]
var ai_next: UnsafePointer[addrinfo]

fn __init__(
inout self,
ai_flags: c_int = 0,
ai_family: c_int = 0,
ai_socktype: c_int = 0,
ai_protocol: c_int = 0,
ai_addrlen: socklen_t = 0,
ai_canonname: UnsafePointer[UInt8] = UnsafePointer[UInt8](),
ai_addr: UnsafePointer[sockaddr] = UnsafePointer[sockaddr](),
ai_next: UnsafePointer[addrinfo] = UnsafePointer[addrinfo](),
):
self.ai_flags = ai_flags
self.ai_family = ai_family
self.ai_socktype = ai_socktype
self.ai_protocol = ai_protocol
self.ai_addrlen = ai_addrlen
self.ai_canonname = ai_canonname
self.ai_addr = ai_addr
self.ai_next = ai_next


# --- ( Network Related Syscalls & Structs )------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion test/data/test_write.csv
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Hello,World,I am here
Hello,World,I am here
Hello,World,I am here
Hello,World,I am here
Hello,World,I am here
Hello,World,I am here
6 changes: 3 additions & 3 deletions test/test_file.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ def test_read():
var file = FileWrapper("test/data/test.txt", "r")
var dest = List[UInt8, True](capacity=16)
_ = file.read(dest)
testing.assert_equal(to_string(dest), "12345")
testing.assert_equal(to_string(dest), "12345\n")


def test_read_all():
# var test = MojoTest("Testing FileWrapper.read_all")
var file = FileWrapper("test/data/test_big_file.txt", "r")
var result = file.read_all()
var bytes = result[0]
testing.assert_equal(len(bytes), 15358)
testing.assert_equal(len(bytes), 15359)
bytes.append(0)

with open("test/data/test_big_file.txt", "r") as f:
Expand All @@ -29,7 +29,7 @@ def test_io_read_all():
var file = FileWrapper("test/data/test_big_file.txt", "r")
var result = read_all(file)
var bytes = result[0]
testing.assert_equal(len(bytes), 15358)
testing.assert_equal(len(bytes), 15359)
bytes.append(0)

with open("test/data/test_big_file.txt", "r") as f:
Expand Down
136 changes: 68 additions & 68 deletions test/test_get_addr.mojo
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
from gojo.net import Socket, TCPAddr, get_ip_address, listen_tcp, dial_tcp
from gojo.syscall import SocketOptions, ProtocolFamily
# from gojo.net import Socket, TCPAddr, get_ip_address, listen_tcp, dial_tcp
# from gojo.syscall import SocketOptions, ProtocolFamily


def test_dial():
# Connect to example.com on port 80 and send a GET request
var connection = dial_tcp("tcp", TCPAddr(get_ip_address("www.example.com"), 80))
var bytes_written: Int = 0
var err = Error()
bytes_written, err = connection.write(
String("GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n").as_bytes_slice()
)
if err:
raise err
# def test_dial():
# # Connect to example.com on port 80 and send a GET request
# var connection = dial_tcp("tcp", TCPAddr(get_ip_address("www.example.com"), 80))
# var bytes_written: Int = 0
# var err = Error()
# bytes_written, err = connection.write(
# String("GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n").as_bytes_slice()
# )
# if err:
# raise err

if bytes_written == 0:
print("No bytes sent to peer.")
return
# if bytes_written == 0:
# print("No bytes sent to peer.")
# return

# Read the response from the connection
var response = List[UInt8, True](capacity=4096)
var bytes_read: Int = 0
bytes_read, err = connection.read(response)
if err:
raise err
# # Read the response from the connection
# var response = List[UInt8, True](capacity=4096)
# var bytes_read: Int = 0
# bytes_read, err = connection.read(response)
# if err:
# raise err

if bytes_read == 0:
print("No bytes received from peer.")
return
# if bytes_read == 0:
# print("No bytes received from peer.")
# return

print(String(response))
# print(String(response))

# Cleanup the connection
err = connection.close()
if err:
raise err
# # Cleanup the connection
# err = connection.close()
# if err:
# raise err


def test_listener():
var listener = listen_tcp("tcp", TCPAddr("0.0.0.0", 8081))
while True:
var conn = listener.accept()
print("Accepted connection from", str(conn.remote_address()))
var err = conn.close()
if err:
raise err
# def test_listener():
# var listener = listen_tcp("tcp", TCPAddr("0.0.0.0", 8081))
# while True:
# var conn = listener.accept()
# print("Accepted connection from", str(conn.remote_address()))
# var err = conn.close()
# if err:
# raise err


# def test_stuff():
# # TODO: context manager not working yet
# # with Socket() as socket:
# # socket.bind("0.0.0.0", 8080)
# # def test_stuff():
# # # TODO: context manager not working yet
# # # with Socket() as socket:
# # # socket.bind("0.0.0.0", 8080)

# var socket = Socket(protocol=ProtocolFamily.PF_UNIX)
# socket.bind("0.0.0.0", 8080)
# socket.connect(get_ip_address("www.example.com"), 80)
# print("File number", socket.file_no())
# var local = socket.get_sock_name()
# var remote = socket.get_peer_name()
# print("Local address", str(local), socket.local_address)
# print("Remote address", str(remote), socket.remote_address)
# socket.set_socket_option(SocketOptions.SO_REUSEADDR, 1)
# print("REUSE_ADDR value", socket.get_socket_option(SocketOptions.SO_REUSEADDR))
# var timeout = 30
# # socket.set_timeout(timeout)
# # print(socket.get_timeout())
# socket.shutdown()
# print("closing")
# var err = socket.close()
# print("closed")
# if err:
# print("err returned")
# raise err
# # var option_value = socket.get_sock_opt(SocketOptions.SO_REUSEADDR)
# # print(option_value)
# # socket.connect(self.ip, self.port)
# # socket.send(message)
# # var response = socket.receive() # TODO: call receive until all data is fetched, receive should also just return bytes
# # socket.shutdown()
# # socket.close()
# # var socket = Socket(protocol=ProtocolFamily.PF_UNIX)
# # socket.bind("0.0.0.0", 8080)
# # socket.connect(get_ip_address("www.example.com"), 80)
# # print("File number", socket.file_no())
# # var local = socket.get_sock_name()
# # var remote = socket.get_peer_name()
# # print("Local address", str(local), socket.local_address)
# # print("Remote address", str(remote), socket.remote_address)
# # socket.set_socket_option(SocketOptions.SO_REUSEADDR, 1)
# # print("REUSE_ADDR value", socket.get_socket_option(SocketOptions.SO_REUSEADDR))
# # var timeout = 30
# # # socket.set_timeout(timeout)
# # # print(socket.get_timeout())
# # socket.shutdown()
# # print("closing")
# # var err = socket.close()
# # print("closed")
# # if err:
# # print("err returned")
# # raise err
# # # var option_value = socket.get_sock_opt(SocketOptions.SO_REUSEADDR)
# # # print(option_value)
# # # socket.connect(self.ip, self.port)
# # # socket.send(message)
# # # var response = socket.receive() # TODO: call receive until all data is fetched, receive should also just return bytes
# # # socket.shutdown()
# # # socket.close()

0 comments on commit f0f27de

Please sign in to comment.