Skip to content

Commit

Permalink
Remove library target and special test features.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinling committed Jun 15, 2024
1 parent b86d678 commit d5cdb54
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:
- name: Test
uses: GabrielBB/xvfb-action@v1
with:
run: cargo test --features=test-ui-replay
run: cargo test

- uses: actions/upload-artifact@v2
with:
Expand Down
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,4 @@ ctor = "0.2.8"
[features]
step-decoder = []
record-ui-test = ["serde", "serde_json"]
test-ui-replay = ["serde", "serde_json"]
debug-region-map = []
test-cynthion = []
23 changes: 0 additions & 23 deletions src/lib.rs

This file was deleted.

36 changes: 32 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
// On Windows, produce a GUI app rather than a console one.
#![windows_subsystem = "windows"]

// We need the bitfield macro.
#[macro_use]
extern crate bitfield;

// We need the ctor macro for the replay test on macOS.
#[cfg(all(test, target_os="macos"))]
#[allow(unused_imports)]
#[macro_use]
extern crate ctor;

// Declare all modules used.
mod backend;
mod capture;
mod compact_index;
mod data_stream;
mod decoder;
mod expander;
mod id;
mod index_stream;
mod model;
mod rcu;
mod row_data;
mod stream;
mod test_cynthion;
mod tree_list_model;
mod ui;
mod usb;
mod util;
mod vec_map;

// Declare optional modules.
#[cfg(any(test, feature="record-ui-test"))]
mod record_ui;
#[cfg(test)]
mod test_replay;

use gtk::prelude::*;
use gtk::gio::ApplicationFlags;

use packetry::ui::{
use ui::{
activate,
display_error,
stop_cynthion
Expand All @@ -29,6 +60,3 @@ fn main() {
}
}

mod test_cynthion;
#[cfg(test)]
mod test_replay;
12 changes: 6 additions & 6 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod imp;

#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
use {
std::cell::RefCell,
std::rc::Rc,
Expand All @@ -26,7 +26,7 @@ glib::wrapper! {

pub trait GenericModel<Item> where Self: Sized {
fn new(capture: CaptureReader,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
on_item_update: Rc<RefCell<dyn FnMut(u32, String)>>)
-> Result<Self, Error>;
fn set_expanded(&self,
Expand All @@ -41,14 +41,14 @@ pub trait GenericModel<Item> where Self: Sized {

impl GenericModel<TrafficItem> for TrafficModel {
fn new(capture: CaptureReader,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
on_item_update: Rc<RefCell<dyn FnMut(u32, String)>>)
-> Result<Self, Error>
{
let model: TrafficModel = glib::Object::new::<TrafficModel>();
let tree = TreeListModel::new(
capture,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
on_item_update)?;
model.imp().tree.replace(Some(tree));
Ok(model)
Expand Down Expand Up @@ -86,14 +86,14 @@ impl GenericModel<TrafficItem> for TrafficModel {

impl GenericModel<DeviceItem> for DeviceModel {
fn new(capture: CaptureReader,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
on_item_update: Rc<RefCell<dyn FnMut(u32, String)>>)
-> Result<Self, Error>
{
let model: DeviceModel = glib::Object::new::<DeviceModel>();
let tree = TreeListModel::new(
capture,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
on_item_update)?;
model.imp().tree.replace(Some(tree));
Ok(model)
Expand Down
8 changes: 4 additions & 4 deletions src/record_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Recording {
action_log: File,
#[cfg(feature="record-ui-test")]
output_log: File,
#[cfg(feature="test-ui-replay")]
#[cfg(test)]
output_log: Option<File>,
view_items: HashMap<String, Vec<String>>,
}
Expand All @@ -66,13 +66,13 @@ impl Recording {
.truncate(true)
.open("output.txt")
.expect("Failed to open UI output log file"),
#[cfg(feature="test-ui-replay")]
#[cfg(test)]
output_log: None,
view_items: HashMap::new(),
}
}

#[cfg(feature="test-ui-replay")]
#[cfg(test)]
pub fn set_output(&mut self, file: File) {
self.output_log = Some(file)
}
Expand All @@ -98,7 +98,7 @@ impl Recording {
fn log_output(&mut self, string: String) {
#[cfg(feature="record-ui-test")]
let output_log = &mut self.output_log;
#[cfg(feature="test-ui-replay")]
#[cfg(test)]
let output_log = self.output_log
.as_mut()
.expect("Recording has no output file set");
Expand Down
6 changes: 3 additions & 3 deletions src/test_cynthion.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use packetry::backend::cynthion::{CynthionDevice, CynthionUsability, Speed};
use packetry::capture::{create_capture, CaptureReader, DeviceId, EndpointId, EndpointTransferId};
use packetry::decoder::Decoder;
use crate::backend::cynthion::{CynthionDevice, CynthionUsability, Speed};
use crate::capture::{create_capture, CaptureReader, DeviceId, EndpointId, EndpointTransferId};
use crate::decoder::Decoder;

use anyhow::{Context, Error};
use futures_lite::future::block_on;
Expand Down
10 changes: 5 additions & 5 deletions src/test_replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use itertools::assert_equal;
use pcap_file::pcap::PcapReader;
use serde_json::Deserializer;

use packetry::decoder::Decoder;
use packetry::model::GenericModel;
use packetry::row_data::{GenericRowData, TrafficRowData, DeviceRowData};
use packetry::record_ui::UiAction;
use packetry::ui::{
use crate::decoder::Decoder;
use crate::model::GenericModel;
use crate::row_data::{GenericRowData, TrafficRowData, DeviceRowData};
use crate::record_ui::UiAction;
use crate::ui::{
UserInterface,
activate,
reset_capture,
Expand Down
8 changes: 4 additions & 4 deletions src/tree_list_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ pub struct TreeListModel<Item, Model, RowData> {
capture: RefCell<CaptureReader>,
root: RootNodeRc<Item>,
regions: RefCell<BTreeMap<u64, Region<Item>>>,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
on_item_update: Rc<RefCell<dyn FnMut(u32, String)>>,
}

Expand All @@ -362,7 +362,7 @@ where Item: 'static + Copy + Debug,
CaptureReader: ItemSource<Item>,
{
pub fn new(mut capture: CaptureReader,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
on_item_update: Rc<RefCell<dyn FnMut(u32, String)>>)
-> Result<Self, Error>
{
Expand All @@ -375,7 +375,7 @@ where Item: 'static + Copy + Debug,
complete: completion.is_complete(),
})),
regions: RefCell::new(BTreeMap::new()),
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
on_item_update,
})
}
Expand Down Expand Up @@ -774,7 +774,7 @@ where Item: 'static + Copy + Debug,
if item_updated {
// The node's description may change.
let summary = cap.summary(&item_node.item)?;
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
if let Ok(position) = u32::try_from(position) {
let mut on_item_update = self.on_item_update.borrow_mut();
on_item_update(position, summary.clone());
Expand Down
30 changes: 15 additions & 15 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use gtk::{
Orientation,
};

#[cfg(not(feature="test-ui-replay"))]
#[cfg(not(test))]
use gtk::{
MessageDialog,
DialogFlags,
Expand Down Expand Up @@ -76,7 +76,7 @@ use crate::row_data::{
DeviceRowData};
use crate::util::{fmt_count, fmt_size};

#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
use {
std::rc::Rc,
crate::record_ui::Recording,
Expand Down Expand Up @@ -272,7 +272,7 @@ pub struct UserInterface {
capture_button: Button,
stop_button: Button,
status_label: Label,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
pub recording: Rc<RefCell<Recording>>,
}

Expand Down Expand Up @@ -336,7 +336,7 @@ pub fn activate(application: &Application) -> Result<(), Error> {
action_bar.pack_start(&stop_button);
action_bar.pack_start(&selector.container);

#[cfg(not(feature="test-ui-replay"))]
#[cfg(not(test))]
window.show();
WINDOW.with(|win_opt| win_opt.replace(Some(window.clone())));

Expand Down Expand Up @@ -403,7 +403,7 @@ pub fn activate(application: &Application) -> Result<(), Error> {
UI.with(|cell| {
cell.borrow_mut().replace(
UserInterface {
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
recording: Rc::new(RefCell::new(
Recording::new(capture.clone()))),
capture,
Expand Down Expand Up @@ -446,7 +446,7 @@ pub fn activate(application: &Application) -> Result<(), Error> {
fn create_view<Item, Model, RowData>(
title: &str,
capture: &CaptureReader,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
recording_args: (&Rc<RefCell<Recording>>, &'static str))
-> (Model, ColumnView)
where
Expand All @@ -456,14 +456,14 @@ fn create_view<Item, Model, RowData>(
CaptureReader: ItemSource<Item>,
Object: ToGenericRowData<Item>
{
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
let (name, expand_rec, update_rec, changed_rec) = {
let (recording, name) = recording_args;
(name, recording.clone(), recording.clone(), recording.clone())
};
let model = Model::new(
capture.clone(),
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
Rc::new(
RefCell::new(
move |position, summary|
Expand Down Expand Up @@ -505,13 +505,13 @@ fn create_view<Item, Model, RowData>(
let model = bind_model.clone();
let node_ref = node_ref.clone();
let list_item = list_item.clone();
#[cfg(any(feature="test-ui-replay",
#[cfg(any(test,
feature="record-ui-test"))]
let recording = expand_rec.clone();
let handler = expander.connect_expanded_notify(move |expander| {
let position = list_item.position();
let expanded = expander.is_expanded();
#[cfg(any(feature="test-ui-replay",
#[cfg(any(test,
feature="record-ui-test"))]
recording.borrow_mut().log_item_expanded(
name, position, expanded);
Expand Down Expand Up @@ -561,7 +561,7 @@ fn create_view<Item, Model, RowData>(
view.append_column(&column);
view.add_css_class("data-table");

#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
model.connect_items_changed(move |model, position, removed, added|
changed_rec.borrow_mut().log_items_changed(
name, model, position, removed, added));
Expand All @@ -576,14 +576,14 @@ pub fn reset_capture() -> Result<CaptureWriter, Error> {
create_view::<TrafficItem, TrafficModel, TrafficRowData>(
"Traffic",
&reader,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
(&ui.recording, "traffic")
);
let (device_model, device_view) =
create_view::<DeviceItem, DeviceModel, DeviceRowData>(
"Devices",
&reader,
#[cfg(any(feature="test-ui-replay", feature="record-ui-test"))]
#[cfg(any(test, feature="record-ui-test"))]
(&ui.recording, "devices")
);
ui.capture = reader;
Expand Down Expand Up @@ -910,7 +910,7 @@ pub fn stop_cynthion() -> Result<(), Error> {
}

pub fn display_error(result: Result<(), Error>) {
#[cfg(not(feature="test-ui-replay"))]
#[cfg(not(test))]
if let Err(e) = result {
use std::fmt::Write;
let mut message = format!("{e}");
Expand Down Expand Up @@ -943,6 +943,6 @@ pub fn display_error(result: Result<(), Error>) {
});
});
}
#[cfg(feature="test-ui-replay")]
#[cfg(test)]
result.unwrap();
}

0 comments on commit d5cdb54

Please sign in to comment.