From 9b6aef6930260918ad7e9fefe1415e63af5b8d4f Mon Sep 17 00:00:00 2001 From: Viet Hoang <1300077+vietqhoang@users.noreply.github.com> Date: Mon, 18 Sep 2023 03:43:37 -1000 Subject: [PATCH 1/3] Add failing tests Added a test case for the more compact version of the pin, where the CDN url is not used. --- .../files/single_quote_outdated_import_map.rb | 1 + ...e_quote_outdated_import_map_without_cdn.rb | 1 + test/npm_test.rb | 28 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 test/fixtures/files/single_quote_outdated_import_map.rb create mode 100644 test/fixtures/files/single_quote_outdated_import_map_without_cdn.rb diff --git a/test/fixtures/files/single_quote_outdated_import_map.rb b/test/fixtures/files/single_quote_outdated_import_map.rb new file mode 100644 index 0000000..fd9942d --- /dev/null +++ b/test/fixtures/files/single_quote_outdated_import_map.rb @@ -0,0 +1 @@ +pin 'md5', to: 'https://cdn.skypack.dev/md5@2.2.0', preload: true diff --git a/test/fixtures/files/single_quote_outdated_import_map_without_cdn.rb b/test/fixtures/files/single_quote_outdated_import_map_without_cdn.rb new file mode 100644 index 0000000..56b2092 --- /dev/null +++ b/test/fixtures/files/single_quote_outdated_import_map_without_cdn.rb @@ -0,0 +1 @@ +pin 'md5', preload: true #@2.2.0 diff --git a/test/npm_test.rb b/test/npm_test.rb index 4c2a956..fd6f122 100644 --- a/test/npm_test.rb +++ b/test/npm_test.rb @@ -18,6 +18,34 @@ class Importmap::NpmTest < ActiveSupport::TestCase end end + test "successful outdated packages using single-quotes with mock" do + npm = Importmap::Npm.new(file_fixture("single_quote_outdated_import_map.rb")) + response = { "dist-tags" => { "latest" => '2.3.0' } }.to_json + + npm.stub(:get_json, response) do + outdated_packages = npm.outdated_packages + + assert_equal(1, outdated_packages.size) + assert_equal('md5', outdated_packages[0].name) + assert_equal('2.2.0', outdated_packages[0].current_version) + assert_equal('2.3.0', outdated_packages[0].latest_version) + end + end + + test "successful outdated packages using single-quotes and without CDN with mock" do + npm = Importmap::Npm.new(file_fixture("single_quote_outdated_import_map_without_cdn.rb")) + response = { "dist-tags" => { "latest" => '2.3.0' } }.to_json + + npm.stub(:get_json, response) do + outdated_packages = npm.outdated_packages + + assert_equal(1, outdated_packages.size) + assert_equal('md5', outdated_packages[0].name) + assert_equal('2.2.0', outdated_packages[0].current_version) + assert_equal('2.3.0', outdated_packages[0].latest_version) + end + end + test "missing outdated packages with mock" do response = { "error" => "Not found" }.to_json From f599242943173cccdf6c2a3d29232f29e8d19d10 Mon Sep 17 00:00:00 2001 From: Viet Hoang <1300077+vietqhoang@users.noreply.github.com> Date: Mon, 18 Sep 2023 03:46:43 -1000 Subject: [PATCH 2/3] Update the regex The failed tests are now passing. The approach is a little naive in that a string surrounded by one double quote and one single quote will return as a positive result. Whether this is important enough that it should be addressed with a more complex regex is up for question. --- lib/importmap/npm.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/importmap/npm.rb b/lib/importmap/npm.rb index 921ac41..553f4b7 100644 --- a/lib/importmap/npm.rb +++ b/lib/importmap/npm.rb @@ -48,8 +48,8 @@ def packages_with_versions # We cannot use the name after "pin" because some dependencies are loaded from inside packages # Eg. pin "buffer", to: "https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.19/nodelibs/browser/buffer.js" - importmap.scan(/^pin .*(?<=npm:|npm\/|skypack\.dev\/|unpkg\.com\/)(.*)(?=@\d+\.\d+\.\d+)@(\d+\.\d+\.\d+(?:[^\/\s"]*)).*$/) | - importmap.scan(/^pin "([^"]*)".* #.*@(\d+\.\d+\.\d+(?:[^\s]*)).*$/) + importmap.scan(/^pin .*(?<=npm:|npm\/|skypack\.dev\/|unpkg\.com\/)(.*)(?=@\d+\.\d+\.\d+)@(\d+\.\d+\.\d+(?:[^\/\s["|']]*)).*$/) | + importmap.scan(/^pin ["|']([^["|']]*)["|'].* #.*@(\d+\.\d+\.\d+(?:[^\s]*)).*$/) end private From 5ca80cfc978b318b8b821e10f9de492fc7715e59 Mon Sep 17 00:00:00 2001 From: Viet Hoang <1300077+vietqhoang@users.noreply.github.com> Date: Mon, 18 Sep 2023 06:30:49 -1000 Subject: [PATCH 3/3] Fix regex The OR seperator is not used appropriately (and not needed) in character group. --- lib/importmap/npm.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/importmap/npm.rb b/lib/importmap/npm.rb index 553f4b7..b47f39e 100644 --- a/lib/importmap/npm.rb +++ b/lib/importmap/npm.rb @@ -48,8 +48,8 @@ def packages_with_versions # We cannot use the name after "pin" because some dependencies are loaded from inside packages # Eg. pin "buffer", to: "https://ga.jspm.io/npm:@jspm/core@2.0.0-beta.19/nodelibs/browser/buffer.js" - importmap.scan(/^pin .*(?<=npm:|npm\/|skypack\.dev\/|unpkg\.com\/)(.*)(?=@\d+\.\d+\.\d+)@(\d+\.\d+\.\d+(?:[^\/\s["|']]*)).*$/) | - importmap.scan(/^pin ["|']([^["|']]*)["|'].* #.*@(\d+\.\d+\.\d+(?:[^\s]*)).*$/) + importmap.scan(/^pin .*(?<=npm:|npm\/|skypack\.dev\/|unpkg\.com\/)(.*)(?=@\d+\.\d+\.\d+)@(\d+\.\d+\.\d+(?:[^\/\s["']]*)).*$/) | + importmap.scan(/^pin ["']([^["']]*)["'].* #.*@(\d+\.\d+\.\d+(?:[^\s]*)).*$/) end private