Skip to content

Commit

Permalink
Add CI. (#3)
Browse files Browse the repository at this point in the history
* Add CI.

* Fix linter + formatting

* Fix linter.

* Formatting

* Update version.
  • Loading branch information
LukeMathWalker authored Nov 15, 2020
1 parent ff5eee8 commit 8cffbcc
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 21 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/audit-on-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Security audit
on:
push:
paths:
- '**/Cargo.toml'
- '**/Cargo.lock'
jobs:
security_audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions-rs/audit-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
71 changes: 71 additions & 0 deletions .github/workflows/general.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Rust

on: [push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings

coverage:
name: Code coverage
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

- name: Run cargo-tarpaulin
uses: actions-rs/[email protected]
with:
args: '--ignore-tests'
12 changes: 12 additions & 0 deletions .github/workflows/scheduled-audit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Security audit
on:
schedule:
- cron: '0 0 * * *'
jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions-rs/audit-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2018"
name = "cargo-manifest"
version = "0.1.0"
version = "0.2.0"
authors = ["Kornel <[email protected]>, Luca Palmieri <[email protected]>"]
description = "Helper crate to parse and manipulate manifests - `Cargo.toml` files."
keywords = ["cargo", "metadata", "toml", "serde", "manifest"]
Expand Down
11 changes: 6 additions & 5 deletions src/afs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ impl<'a> Filesystem<'a> {

impl<'a> AbstractFilesystem for Filesystem<'a> {
fn file_names_in(&self, rel_path: &str) -> io::Result<HashSet<Box<str>>> {
Ok(read_dir(self.path.join(rel_path))?.filter_map(|entry| {
entry.ok().map(|e| {
e.file_name().to_string_lossy().to_string().into_boxed_str()
Ok(read_dir(self.path.join(rel_path))?
.filter_map(|entry| {
entry
.ok()
.map(|e| e.file_name().to_string_lossy().to_string().into_boxed_str())
})
})
.collect())
.collect())
}
}
34 changes: 24 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![allow(clippy::large_enum_variant)]
//! This crate defines `struct`s that can be deserialized with Serde
//! to load and inspect `Cargo.toml` metadata.
//!
//! See `Manifest::from_slice`.
use std::fs;
use std::io;
use std::path::Path;
use toml;

#[macro_use]
extern crate serde_derive;
Expand All @@ -24,6 +24,7 @@ mod afs;
mod error;
pub use crate::afs::*;
pub use crate::error::Error;
use std::str::FromStr;

/// The top-level `Cargo.toml` structure
///
Expand Down Expand Up @@ -102,13 +103,17 @@ impl Manifest<Value> {
pub fn from_path(cargo_toml_path: impl AsRef<Path>) -> Result<Self, Error> {
Self::from_path_with_metadata(cargo_toml_path)
}
}

impl FromStr for Manifest<Value> {
type Err = Error;

/// Parse contents of a `Cargo.toml` file loaded as a string
///
/// Note: this is **not** a file name, but file's content. See `from_path`.
///
/// It does not call `complete_from_path`, so may be missing implicit data.
pub fn from_str(cargo_toml_content: &str) -> Result<Self, Error> {
fn from_str(cargo_toml_content: &str) -> Result<Self, Self::Err> {
Self::from_slice_with_metadata(cargo_toml_content.as_bytes())
}
}
Expand Down Expand Up @@ -148,7 +153,9 @@ impl<Metadata: for<'a> Deserialize<'a>> Manifest<Metadata> {
///
/// This scans the disk to make the data in the manifest as complete as possible.
pub fn complete_from_path(&mut self, path: &Path) -> Result<(), Error> {
let manifest_dir = path.parent().ok_or_else(|| io::Error::new(io::ErrorKind::Other, "bad path"))?;
let manifest_dir = path
.parent()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "bad path"))?;
self.complete_from_abstract_filesystem(Filesystem::new(manifest_dir))
}

Expand Down Expand Up @@ -373,7 +380,7 @@ impl Dependency {
pub fn req(&self) -> &str {
match *self {
Dependency::Simple(ref v) => v,
Dependency::Detailed(ref d) => d.version.as_ref().map(|s| s.as_str()).unwrap_or("*"),
Dependency::Detailed(ref d) => d.version.as_deref().unwrap_or("*"),
}
}

Expand All @@ -393,13 +400,13 @@ impl Dependency {
pub fn package(&self) -> Option<&str> {
match *self {
Dependency::Simple(_) => None,
Dependency::Detailed(ref d) => d.package.as_ref().map(|p| p.as_str()),
Dependency::Detailed(ref d) => d.package.as_deref(),
}
}

// Git URL of this dependency, if any
pub fn git(&self) -> Option<&str> {
self.detail().and_then(|d| d.git.as_ref().map(|p| p.as_str()))
self.detail().and_then(|d| d.git.as_deref())
}

// `true` if it's an usual crates.io dependency,
Expand All @@ -409,7 +416,13 @@ impl Dependency {
Dependency::Simple(_) => true,
Dependency::Detailed(ref d) => {
// TODO: allow registry to be set to crates.io explicitly?
d.path.is_none() && d.registry.is_none() && d.registry_index.is_none() && d.git.is_none() && d.tag.is_none() && d.branch.is_none() && d.rev.is_none()
d.path.is_none()
&& d.registry.is_none()
&& d.registry_index.is_none()
&& d.git.is_none()
&& d.tag.is_none()
&& d.branch.is_none()
&& d.rev.is_none()
}
}
}
Expand Down Expand Up @@ -499,7 +512,7 @@ impl PartialEq<Publish> for bool {
fn eq(&self, p: &Publish) -> bool {
match *p {
Publish::Flag(flag) => flag == *self,
Publish::Registry(ref reg) => !reg.is_empty() == *self,
Publish::Registry(ref reg) => reg.is_empty() != *self,
}
}
}
Expand All @@ -526,8 +539,9 @@ fn default_master() -> String {
}

fn ok_or_default<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where T: Deserialize<'de> + Default,
D: Deserializer<'de>
where
T: Deserialize<'de> + Default,
D: Deserializer<'de>,
{
Ok(Deserialize::deserialize(deserializer).unwrap_or_default())
}
Expand Down
28 changes: 23 additions & 5 deletions tests/parse.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use cargo_manifest as lib;
use cargo_manifest::Manifest;
use std::fs::read;
use std::str::FromStr;
use toml;

#[test]
fn own() {
let m = Manifest::from_slice(&read("Cargo.toml").unwrap()).unwrap();
let package = m.package.as_ref().unwrap();
assert_eq!("cargo-manifest", package.name);
let m = Manifest::<toml::Value>::from_slice_with_metadata(&read("Cargo.toml").unwrap()).unwrap();
let m =
Manifest::<toml::Value>::from_slice_with_metadata(&read("Cargo.toml").unwrap()).unwrap();
let package = m.package.as_ref().unwrap();
assert_eq!("cargo-manifest", package.name);
assert_eq!(lib::Edition::E2018, package.edition);
Expand All @@ -19,7 +21,17 @@ fn opt_level() {
let m = Manifest::from_slice(&read("tests/opt_level.toml").unwrap()).unwrap();
let package = m.package.as_ref().unwrap();
assert_eq!("byteorder", package.name);
assert_eq!(3, m.profile.unwrap().bench.unwrap().opt_level.unwrap().as_integer().unwrap());
assert_eq!(
3,
m.profile
.unwrap()
.bench
.unwrap()
.opt_level
.unwrap()
.as_integer()
.unwrap()
);
assert_eq!(false, m.lib.unwrap().bench);
assert_eq!(lib::Edition::E2015, package.edition);
assert_eq!(1, m.patch.unwrap().len());
Expand All @@ -34,7 +46,10 @@ fn autobin() {
assert!(package.autobins);
assert!(m.lib.is_none());
assert_eq!(1, m.bin.as_ref().unwrap().len());
assert_eq!(Some("auto-bin"), m.bin.unwrap()[0].name.as_ref().map(|s| s.as_str()));
assert_eq!(
Some("auto-bin"),
m.bin.unwrap()[0].name.as_ref().map(|s| s.as_str())
);
}

#[test]
Expand All @@ -60,10 +75,13 @@ fn metadata() {

#[test]
fn legacy() {
let m = Manifest::from_slice(br#"[project]
let m = Manifest::from_slice(
br#"[project]
name = "foo"
version = "1"
"#).expect("parse old");
"#,
)
.expect("parse old");
let package = m.package.as_ref().unwrap();
assert_eq!("foo", package.name);
let m = Manifest::from_str("name = \"foo\"\nversion=\"1\"").expect("parse bare");
Expand Down

0 comments on commit 8cffbcc

Please sign in to comment.