solang_import_resolution_tests.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: Apache-2.0
  2. use assert_cmd::Command;
  3. use std::path::PathBuf;
  4. fn solang_import_resolution_tests() -> PathBuf {
  5. PathBuf::from(env!("CARGO_MANIFEST_DIR"))
  6. .join("testdata")
  7. .join("solang_import_resolution_tests")
  8. }
  9. fn make_run(dir: &str) -> Command {
  10. let current_dir = solang_import_resolution_tests().join(dir);
  11. let mut cmd = Command::cargo_bin("solang").unwrap();
  12. cmd.current_dir(current_dir)
  13. .args(["compile", "--target", "solana"]);
  14. cmd
  15. }
  16. #[test]
  17. fn import_test_01_solang_remap_target() {
  18. // Command 1
  19. let mut cmd = make_run("01_solang_remap_target");
  20. let run = cmd.arg("contracts/Contract.sol").env("exit", "1").assert();
  21. let stderr = String::from_utf8_lossy(&run.get_output().stderr);
  22. println!("stderr: {stderr}");
  23. assert!(stderr.contains("file not found 'lib/Lib.sol'"));
  24. assert!(stderr.contains("'Lib' not foun"));
  25. assert!(stderr.contains("'x' not found"));
  26. // Command 2
  27. let mut cmd = make_run("01_solang_remap_target");
  28. let run = cmd
  29. .arg("contracts/Contract.sol")
  30. .arg("--importmap")
  31. .arg("lib=node_modules/lib")
  32. .assert()
  33. .failure();
  34. let stderr = String::from_utf8_lossy(&run.get_output().stderr);
  35. println!("stderr: {stderr}");
  36. assert!(stderr.contains("file not found 'lib/Lib.sol'"));
  37. assert!(stderr.contains("'Lib' not foun"));
  38. assert!(stderr.contains("'x' not found"));
  39. // Command 3
  40. let mut cmd = make_run("01_solang_remap_target");
  41. let run = cmd
  42. .arg("contracts/Contract.sol")
  43. .arg("--importmap")
  44. .arg("lib=node_modules/lib")
  45. .arg("--importpath")
  46. .arg(".")
  47. .assert()
  48. .failure();
  49. let stderr = String::from_utf8_lossy(&run.get_output().stderr);
  50. println!("stderr: {stderr}");
  51. assert!(stderr.contains("file not found 'lib/Lib.sol'"));
  52. assert!(stderr.contains("'Lib' not foun"));
  53. assert!(stderr.contains("'x' not found"));
  54. // Command 4
  55. let mut cmd = make_run("01_solang_remap_target");
  56. let run = cmd
  57. .arg("contracts/Contract.sol")
  58. .arg("--importmap")
  59. .arg("lib=node_modules/lib")
  60. .arg("--importpath")
  61. .arg(".")
  62. .arg("--importpath")
  63. .arg("resources/node_modules")
  64. .assert()
  65. .failure();
  66. let stderr = String::from_utf8_lossy(&run.get_output().stderr);
  67. println!("stderr: {stderr}");
  68. assert!(stderr.contains("file not found 'lib/Lib.sol'"));
  69. assert!(stderr.contains("'Lib' not foun"));
  70. assert!(stderr.contains("'x' not found"));
  71. // Command 5
  72. let mut cmd = make_run("01_solang_remap_target");
  73. cmd.arg("contracts/Contract.sol")
  74. .arg("--importmap")
  75. .arg("lib=node_modules/lib")
  76. .arg("--importpath")
  77. .arg(".")
  78. .arg("--importpath")
  79. .arg("resources")
  80. .assert()
  81. .success();
  82. }
  83. #[test]
  84. fn import_test_02_solang_incorrect_direct_imports() {
  85. // Command 1
  86. let mut cmd = make_run("02_solang_incorrect_direct_imports");
  87. cmd.arg("contracts/Contract.sol").env("exit", "0").assert();
  88. // Command 2
  89. let mut cmd = make_run("02_solang_incorrect_direct_imports");
  90. cmd.arg("contracts/Contract.sol")
  91. .arg("--importpath")
  92. .arg(".")
  93. .assert()
  94. .success();
  95. }
  96. #[test]
  97. fn import_test_03_ambiguous_imports_should_fail() {
  98. // Command 1
  99. let mut cmd = make_run("03_ambiguous_imports_should_fail");
  100. cmd.arg("contracts/Contract.sol")
  101. .arg("--importmap")
  102. .arg("lib=resources/node_modules/lib")
  103. .arg("--importpath")
  104. .arg("contracts")
  105. .arg("--importpath")
  106. .arg(".")
  107. .assert()
  108. .failure();
  109. }
  110. #[test]
  111. fn import_test_04_multiple_path_map_segments() {
  112. // Command 1
  113. let mut cmd = make_run("04_multiple_map_path_segments");
  114. cmd.arg("contracts/Contract.sol")
  115. .arg("--importmap")
  116. .arg("lib/nested=resources/node_modules/lib/nested")
  117. .arg("--importpath")
  118. .arg(".")
  119. .assert()
  120. .success();
  121. }
  122. #[test]
  123. fn import_test_06_redundant_remaps() {
  124. // Command 1
  125. let mut cmd = make_run("06_redundant_remaps");
  126. cmd.arg("contracts/Contract.sol")
  127. .arg("--importmap")
  128. .arg("node_modules=resources/node_modules")
  129. .arg("--importmap")
  130. .arg("node_modules=node_modules")
  131. .arg("--importpath")
  132. .arg("resources")
  133. .assert()
  134. .success();
  135. let mut cmd = make_run("06_redundant_remaps");
  136. cmd.arg("contracts/Contract.sol")
  137. .arg("--importmap")
  138. .arg("node_modules=node_modules")
  139. .arg("--importmap")
  140. .arg("node_modules=resources/node_modules")
  141. .arg("--importpath")
  142. .arg("resources")
  143. .assert()
  144. .failure();
  145. let mut cmd = make_run("06_redundant_remaps");
  146. cmd.arg("contracts/Contract.sol")
  147. .arg("--importmap")
  148. .arg("node_modules=node_modules")
  149. .arg("--importmap")
  150. .arg("node_modules=resources/node_modules")
  151. .arg("--importmap")
  152. .arg("node_modules=node_modules")
  153. .arg("--importpath")
  154. .arg("resources")
  155. .assert()
  156. .success();
  157. }