Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/sh

echo "Running pre-commit checks..."
# Format all Rust files
echo "\nChecking formatting..."
cargo fmt -- --check
if [ $? -ne 0 ]; then
echo "Formatting check failed. Please run 'cargo fmt' and commit again."
exit 1
fi

# Check compilation
echo "\nChecking compilation..."
cargo check
if [ $? -ne 0 ]; then
echo "Compilation check failed. Please fix the errors and commit again."
exit 1
fi

echo "\nChecking release compilation..."
cargo check --release
if [ $? -ne 0 ]; then
echo "Release compilation check failed. Please fix the errors and commit again."
exit 1
fi

# Run clippy
echo "\nRunning clippy..."
cargo clippy -- -D warnings
if [ $? -ne 0 ]; then
echo "Clippy check failed. Please fix the warnings and commit again."
exit 1
fi

# Run all tests
echo "\nRunning tests..."
cargo test
if [ $? -ne 0 ]; then
echo "Tests failed. Please fix the failing tests and commit again."
exit 1
fi

echo "\nAll checks completed successfully."
exit 0
59 changes: 59 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Chat Server Test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Build server and client
run: cargo build

- name: run tests
run: cargo test

- name: Set log level
run: export RUST_LOG=info

- name: Run server
run: cargo run --bin server 0.0.0.0:12345 &

- name: Wait for server to start
run: sleep 5

- name: Run client and send test message
run: |
cargo run --bin client 127.0.0.1:12345 TestUser > client_log.txt 2>&1 &
sleep 5 # Give some time for the message to be sent and processed

- name: Check client logs
run: |
if grep -q "Welcome to the chat, TestUser!" client_log.txt; then
echo "client connected to server"
else
echo "client did not connect to server"
cat client_log.txt
exit 1
fi

- name: Check for failures
run: |
if [ $? -ne 0 ]; then
echo "Client or server exited with a failure"
exit 1
else
echo "Client and server ran successfully"
fi
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

*.vscode
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]
members = [
"server",
"client",
"common",
]

resolver = "2"
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,27 @@ without error, and is free of clippy errors.
send a message to the server from the client. Make sure that niether the server
or client exit with a failure. This action should be run anytime new code
is pushed to a branch or landed on the main branch.

## Run pre-commit hook:
```
sh .githooks/pre-commit
```

## Run tests:
```
cargo test
```

## Launch server:
```
cargo run --bin server <ip>:<port>
```

## Launch client:
```
cargo run --bin client <ip>:<port> <username>
```

## GitHub Actions

The GitHub Actions workflow is defined in `.github/workflows/ci.yml`. It will run the server and client and check if the client can send a message to the server.
2 changes: 2 additions & 0 deletions client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
RUST_LOG=info cargo run --bin client $1:$2 $3
15 changes: 15 additions & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "client"
version = "0.1.0"
edition = "2021"

[dependencies]
env_logger = "0.11.5"
futures-util = "0.3.31"
log = "0.4.22"
tokio = { version = "1.40.0", features = ["full"] }
tokio-tungstenite = "0.24.0"
common = { path = "../common" }
[dev-dependencies]
tokio-test = "0.4.4"
futures = "0.3.31"
Loading