Skip to content

Commit

Permalink
fix & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SRetip committed Mar 27, 2024
1 parent 94a0c77 commit b58e495
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 46 deletions.
50 changes: 25 additions & 25 deletions Readme.md

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions module/move/willbe/src/action/readme_health_table_renew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ mod private
user_and_repo: String,
/// List of branches in the repository.
branches: Option< Vec< String > >,
/// workspace root
workspace_root : String,
}

/// Structure that holds the parameters for generating a table.
Expand Down Expand Up @@ -198,7 +200,7 @@ mod private
{
user_and_repo = url::git_info_extract( core_url )?;
}
Ok( Self { core_url: core_url.unwrap_or_default(), user_and_repo, branches } )
Ok( Self { core_url: core_url.unwrap_or_default(), user_and_repo, branches, workspace_root: path.to_string_lossy().to_string() } )
}
}

Expand Down Expand Up @@ -381,11 +383,15 @@ mod private
}
if table_parameters.include
{
let path = table_parameters.base_path.replace( "/", "%2F" );
let p = Path::new( table_parameters.base_path.as_str() ).join( &module_name );
let path = Path::new( table_parameters.base_path.as_str() ).join( &module_name );
let p = Path::new( &parameters.workspace_root ).join( &path );
// let path = table_parameters.base_path.
let example = if let Some( name ) = find_example_file( p.as_path(), &module_name )
{
format!( "[![Open in Gitpod](https://raster.shields.io/static/v1?label=&message=try&color=eee)](https://gitpod.io/#RUN_PATH=.,SAMPLE_FILE={path}%2F{}%2Fexamples%2F{},RUN_POSTFIX=--example%20{}/{})", &module_name, name, name, parameters.core_url )
let path = path.to_string_lossy().replace( "/", "\\" ).replace( "\\", "%2F" );
let file_name = name.split( "\\" ).last().unwrap();
let name = file_name.strip_suffix( ".rs" ).unwrap();
format!( "[![Open in Gitpod](https://raster.shields.io/static/v1?label=&message=try&color=eee)](https://gitpod.io/#RUN_PATH=.,SAMPLE_FILE={path}%2Fexamples%2F{file_name},RUN_POSTFIX=--example%20{name}/{})", parameters.core_url )
}
else
{
Expand All @@ -401,7 +407,7 @@ mod private
{
let examples_dir = base_path.join("examples" );

if examples_dir.exists() && examples_dir.is_dir()
if examples_dir.exists() && examples_dir.is_dir()
{
if let Ok( entries ) = std::fs::read_dir( &examples_dir )
{
Expand Down
18 changes: 9 additions & 9 deletions module/move/willbe/src/action/readme_modules_headers_renew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod private
{

/// Create `ModuleHeader` instance from the folder where Cargo.toml is stored.
fn from_cargo_toml( package : Package, default_discord_url : &Option< String >, workspace_path : &str ) -> Result< Self >
fn from_cargo_toml( package : Package, default_discord_url : &Option< String > ) -> Result< Self >
{
let stability = package.stability()?;

Expand All @@ -53,7 +53,7 @@ mod private
(
Self
{
module_path: package.manifest_path().parent().unwrap().as_ref().strip_prefix( workspace_path ).unwrap().to_path_buf(),
module_path: package.manifest_path().parent().unwrap().as_ref().to_path_buf(),
stability,
module_name,
repository_url,
Expand All @@ -63,17 +63,17 @@ mod private
}

/// Convert `ModuleHeader`to header.
fn to_header( self ) -> Result< String >
fn to_header( self, workspace_path : &str ) -> Result< String >
{
let discord = self.discord_url.map( | discord_url |
format!( " [![discord](https://img.shields.io/discord/872391416519737405?color=eee&logo=discord&logoColor=eee&label=ask)]({discord_url})" )
)
.unwrap_or_default();
let path = self.module_path.to_string_lossy().replace( "/", "%2F" );

let repo_url = url::extract_repo_url( &self.repository_url ).and_then( | r | url::git_info_extract( &r ).ok() ).ok_or_else::< Error, _ >( || err!( "Fail to parse repository url" ) )?;
let example = if let Some( name ) = find_example_file( self.module_path.as_path(), &self.module_name )
{
let p = name.replace( "\\","%2F");
let p = name.strip_prefix( workspace_path ).unwrap().get( 1.. ).unwrap().replace( "\\","%2F" );
let name = name.split( "\\" ).last().unwrap().split( "." ).next().unwrap();
format!( " [![Open in Gitpod](https://raster.shields.io/static/v1?label=&message=try&color=eee)](https://gitpod.io/#RUN_PATH=.,SAMPLE_FILE={p},RUN_POSTFIX=--example%20{}/https://github.com/{})", name, repo_url )
}
Expand Down Expand Up @@ -130,7 +130,7 @@ mod private
.join( readme_path( path.parent().unwrap().as_ref() ).ok_or_else::< Error, _ >( || err!( "Fail to find README.md" ) )? );

let pakage = Package::try_from( path )?;
let header = ModuleHeader::from_cargo_toml( pakage, &discord_url, cargo_metadata.workspace_root()?.to_str().unwrap() )?;
let header = ModuleHeader::from_cargo_toml( pakage, &discord_url )?;

let mut file = OpenOptions::new()
.read( true )
Expand All @@ -150,7 +150,7 @@ mod private

_ = query::parse( raw_params )?;

let content = header_content_generate( &content, header, raw_params )?;
let content = header_content_generate( &content, header, raw_params, cargo_metadata.workspace_root()?.to_str().unwrap() )?;

file.set_len( 0 )?;
file.seek( SeekFrom::Start( 0 ) )?;
Expand All @@ -159,9 +159,9 @@ mod private
Ok( () )
}

fn header_content_generate< 'a >( content : &'a str, header : ModuleHeader, raw_params : &str ) -> Result< Cow< 'a, str > >
fn header_content_generate< 'a >( content : &'a str, header : ModuleHeader, raw_params : &str, workspace_root : &str ) -> Result< Cow< 'a, str > >
{
let header = header.to_header()?;
let header = header.to_header( workspace_root )?;
let result = TAGS_TEMPLATE.get().unwrap().replace( &content, &format!( "<!--{{ generate.module_header.start{raw_params} }}-->\n{header}\n<!--{{ generate.module_header.end }}-->" ) );
Ok( result )
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main()
{
print!( "example" );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[workspace]
resolver = "2"
members = [
"module/test_module",
]

[workspace.metadata]
master_branch = "test_branch"
project_name = "test"
repo_url = "https://github.com/Username/test"
discord_url = "https://discord.gg/m3YfbXpUUY"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--{ generate.main_header.start() }-->
<!--{ generate.main_header.end }-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_module"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/Wandalen/wTools/tree/master/module/move/test_module"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--{ generate.module_header.start() }-->
<!--{ generate.module_header.end }-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main()
{
println!( "example" );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub fn add( left : usize, right : usize ) -> usize
{
left + right
}

#[ cfg( test ) ]
mod tests
{
use super::*;

#[ test ]
fn it_works()
{
let result = add( 2, 2 );
assert_eq!( result, 4 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ fn stability_and_repository_from_module_toml()
_ = file.read_to_string( &mut actual ).unwrap();

assert!( actual.contains( "[![stability-stable](https://img.shields.io/badge/stability-stable-green.svg)](https://github.com/emersion/stability-badges#stable)" ) );
assert!( actual.contains( "https://github.com/Testusername/TestProject" ) );
}

#[ test ]
Expand Down Expand Up @@ -198,5 +197,5 @@ fn sample_cell()
let mut actual = String::new();
_ = file.read_to_string( &mut actual ).unwrap();

assert!( actual.contains( "[![Open in Gitpod](https://raster.shields.io/static/v1?label=&message=try&color=eee)](https://gitpod.io/#RUN_PATH=.,SAMPLE_FILE=sample%2Frust%2F_willbe_variadic_tag_configurations_c_trivial%2Fsrc%2Fmain.rs,RUN_POSTFIX=--example%20_willbe_variadic_tag_configurations_c_trivial/https://github.com/SomeName/SomeCrate/C)" ) );
assert!( actual.contains( " [![Open in Gitpod](https://raster.shields.io/static/v1?label=&message=try&color=eee)](https://gitpod.io/#RUN_PATH=.,SAMPLE_FILE=.%2F_willbe_variadic_tag_configurations_c%2Fexamples%2F_willbe_variadic_tag_configurations_c_trivial.rs,RUN_POSTFIX=--example%20_willbe_variadic_tag_configurations_c_trivial/https://github.com/SomeName/SomeCrate/C)" ) );
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,38 @@ fn docs()
}

#[ test ]
fn gitpod()
fn no_gitpod()
{
// Arrange
let temp = arrange( "single_module" );
let temp = arrange("single_module");

// Act
_ = action::readme_modules_headers_renew(AbsolutePath::try_from(temp.path()).unwrap()).unwrap();
let mut file = std::fs::File::open(temp.path().join("test_module").join("Readme.md")).unwrap();

let mut actual = String::new();

_ = file.read_to_string(&mut actual).unwrap();

// Assert
// no example - no gitpod
assert!(!actual.contains("[Open in Gitpod]"));
}
#[ test ]
fn with_gitpod()
{
let temp = arrange( "single_module_with_example" );

// Act
_ = action::readme_modules_headers_renew( AbsolutePath::try_from( temp.path() ).unwrap() ).unwrap();
let mut file = std::fs::File::open( temp.path().join( "test_module" ).join( "Readme.md" ) ).unwrap();
let mut file = std::fs::File::open( temp.path().join( "module" ).join( "test_module" ).join( "Readme.md" ) ).unwrap();

let mut actual = String::new();

_ = file.read_to_string( &mut actual ).unwrap();

// Assert
assert!( actual.contains( "[![Open in Gitpod](https://raster.shields.io/static/v1?label=try&message=online&color=eee&logo=gitpod&logoColor=eee)](https://gitpod.io/#RUN_PATH=.,SAMPLE_FILE=sample%2Frust%2Ftest_module_trivial%2Fsrc%2Fmain.rs,RUN_POSTFIX=--example%20test_module_trivial/https://github.com/Wandalen/wTools)" ) );
dbg!(&actual);
assert!( actual.contains( "[Open in Gitpod]" ) );
}

#[ test ]
Expand Down

0 comments on commit b58e495

Please sign in to comment.