Skip to content

Commit 5b9ac58

Browse files
committed
fix the tests for windows
1 parent b75ede0 commit 5b9ac58

File tree

2 files changed

+105
-21
lines changed

2 files changed

+105
-21
lines changed

tests/acceptance/execute_binary.rs

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::path::PathBuf;
22

33
use crate::support::sandbox::{sandbox, PackageBinInfo};
4+
use cfg_if::cfg_if;
45
use hamcrest2::assert_that;
56
use hamcrest2::prelude::*;
67
use test_support::matchers::execs;
@@ -20,45 +21,92 @@ const PKG_CONFIG_BASIC: &str = r#"{
2021
"manager": "Npm"
2122
}"#;
2223

23-
// TODO: windows, ugh
2424
fn node_bin(version: &str) -> String {
25-
format!(
26-
r#"#!/bin/sh
25+
cfg_if! {
26+
if #[cfg(target_os = "windows")] {
27+
format!(
28+
r#"@echo off
29+
echo Node version {}
30+
echo node args: %*
31+
"#,
32+
version
33+
)
34+
} else {
35+
format!(
36+
r#"#!/bin/sh
2737
echo "Node version {}"
2838
echo "node args: $@"
2939
"#,
30-
version
31-
)
40+
version
41+
)
42+
}
43+
}
3244
}
3345

3446
fn npm_bin(version: &str) -> String {
35-
format!(
36-
r#"#!/bin/sh
47+
cfg_if! {
48+
if #[cfg(target_os = "windows")] {
49+
format!(
50+
r#"@echo off
51+
echo Npm version {}
52+
echo npm args: %*
53+
"#,
54+
version
55+
)
56+
} else {
57+
format!(
58+
r#"#!/bin/sh
3759
echo "Npm version {}"
3860
echo "npm args: $@"
3961
"#,
40-
version
41-
)
62+
version
63+
)
64+
}
65+
}
4266
}
4367

4468
fn yarn_bin(version: &str) -> String {
45-
format!(
46-
r#"#!/bin/sh
69+
cfg_if! {
70+
if #[cfg(target_os = "windows")] {
71+
format!(
72+
r#"@echo off
73+
echo Yarn version {}
74+
echo yarn args: %*
75+
"#,
76+
version
77+
)
78+
} else {
79+
format!(
80+
r#"#!/bin/sh
4781
echo "Yarn version {}"
4882
echo "yarn args: $@"
4983
"#,
50-
version
51-
)
84+
version
85+
)
86+
}
87+
}
5288
}
5389

5490
fn cowsay_bin(name: &str, version: &str) -> String {
55-
format!(
56-
r#"#!/bin/sh
91+
cfg_if! {
92+
if #[cfg(target_os = "windows")] {
93+
format!(
94+
r#"@echo off
95+
echo {} version {}
96+
echo {} args: %*
97+
"#,
98+
name, version, name
99+
)
100+
} else {
101+
format!(
102+
r#"#!/bin/sh
57103
echo "{} version {}"
58104
echo "{} args: $@"
59105
"#,
60-
name, version, name
61-
)
106+
name, version, name
107+
)
108+
}
109+
}
62110
}
63111

64112
fn cowsay_bin_info(version: &str) -> Vec<PackageBinInfo> {

tests/acceptance/support/sandbox.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io::{Read, Write};
55
use std::path::{Path, PathBuf};
66
use std::time::{Duration, SystemTime};
77

8+
use cfg_if::cfg_if;
89
use hyperx::header::HttpDate;
910
use mockito::{self, mock, Matcher};
1011
use semver::Version;
@@ -495,7 +496,13 @@ impl SandboxBuilder {
495496
));
496497
if let Some(bin_infos) = bins {
497498
for bin_info in bin_infos.iter() {
498-
let bin_path = package_img_dir.join("bin").join(&bin_info.name);
499+
cfg_if! {
500+
if #[cfg(target_os = "windows")] {
501+
let bin_path = package_img_dir.join(format!("{}.cmd", &bin_info.name));
502+
} else {
503+
let bin_path = package_img_dir.join("bin").join(&bin_info.name);
504+
}
505+
}
499506
self.files
500507
.push(FileBuilder::new(bin_path, &bin_info.contents).make_executable());
501508
}
@@ -507,6 +514,14 @@ impl SandboxBuilder {
507514
pub fn project_bins(mut self, bins: Vec<PackageBinInfo>) -> Self {
508515
let project_bin_dir = self.root().join("node_modules").join(".bin");
509516
for bin_info in bins.iter() {
517+
cfg_if! {
518+
if #[cfg(target_os = "windows")] {
519+
// in Windows, binaries have an extra file with an executable extension
520+
let win_bin_path = project_bin_dir.join(format!("{}.cmd", &bin_info.name));
521+
self.files.push(FileBuilder::new(win_bin_path, &bin_info.contents).make_executable());
522+
}
523+
}
524+
// Volta on both Windows and Unix checks for the existence of the binary with no extension
510525
let bin_path = project_bin_dir.join(&bin_info.name);
511526
self.files
512527
.push(FileBuilder::new(bin_path, &bin_info.contents).make_executable());
@@ -528,23 +543,44 @@ impl SandboxBuilder {
528543
npm_version: &str,
529544
contents: &str,
530545
) -> Self {
531-
let node_bin_file = node_image_dir(node_version).join("bin").join("node");
546+
cfg_if! {
547+
if #[cfg(target_os = "windows")] {
548+
let node_file = "node.cmd";
549+
} else {
550+
let node_file = "node";
551+
}
552+
}
553+
let node_bin_file = node_image_dir(node_version).join("bin").join(node_file);
532554
self.files
533555
.push(FileBuilder::new(node_bin_file, contents).make_executable());
534556
self.node_npm_version_file(node_version, npm_version)
535557
}
536558

537559
/// Write an executable npm binary with the input contents (chainable)
538560
pub fn setup_npm_binary(mut self, version: &str, contents: &str) -> Self {
539-
let npm_bin_file = npm_image_dir(version).join("bin").join("npm");
561+
cfg_if! {
562+
if #[cfg(target_os = "windows")] {
563+
let npm_file = "npm.cmd";
564+
} else {
565+
let npm_file = "npm";
566+
}
567+
}
568+
let npm_bin_file = npm_image_dir(version).join("bin").join(npm_file);
540569
self.files
541570
.push(FileBuilder::new(npm_bin_file, contents).make_executable());
542571
self
543572
}
544573

545574
/// Write an executable yarn binary with the input contents (chainable)
546575
pub fn setup_yarn_binary(mut self, version: &str, contents: &str) -> Self {
547-
let yarn_bin_file = yarn_image_dir(version).join("bin").join("yarn");
576+
cfg_if! {
577+
if #[cfg(target_os = "windows")] {
578+
let yarn_file = "yarn.cmd";
579+
} else {
580+
let yarn_file = "yarn";
581+
}
582+
}
583+
let yarn_bin_file = yarn_image_dir(version).join("bin").join(yarn_file);
548584
self.files
549585
.push(FileBuilder::new(yarn_bin_file, contents).make_executable());
550586
self

0 commit comments

Comments
 (0)