Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Periphery 2.18.0 command-line parsing strategy #183

Merged
merged 7 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,21 @@ require:
AllCops:
TargetRubyVersion: 2.7
NewCops: enable

Metrics/AbcSize:
Max: 21

Metrics/CyclomaticComplexity:
Max: 10

Metrics/MethodLength:
Max: 15

Metrics/PerceivedComplexity:
Max: 10

RSpec/MultipleMemoizedHelpers:
Max: 6

RSpec/NestedGroups:
Max: 4
19 changes: 18 additions & 1 deletion lib/periphery/runner.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'open3'
require 'rubygems/version'

module Periphery
class Runner # :nodoc:
Expand All @@ -23,10 +24,26 @@ def scan_arguments(options)
next unless value

value = nil if value.is_a?(TrueClass)
value = value.join(',') if value.is_a?(Array)
if value.is_a?(Array)
if Gem::Version.new(version) >= Gem::Version.new('2.18.0')
new_options << "--#{key.to_s.tr('_', '-')}"
new_options.push(*value.map(&:to_s))
next
else
value = value.join(',')
end
end
new_options << "--#{key.to_s.tr('_', '-')}"
new_options << value&.to_s if value
end
end

def version
arguments = [binary_path, 'version']
stdout, stderr, status = Open3.capture3(*arguments)
raise "error: #{arguments} existed with status code #{status.exitstatus}. #{stderr}" unless status.success?

stdout.strip
end
end
end
15 changes: 13 additions & 2 deletions spec/danger/danger_periphery_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
let(:periphery_options) do
{
project: fixture('test.xcodeproj'),
targets: 'test',
targets: targets,
schemes: 'test',
skip_build: true,
index_store_path: index_store_path
}
end
let(:targets) { 'test' }

before do
periphery.binary_path = binary('periphery')
Expand All @@ -32,11 +33,11 @@
deleted_files: [],
added_files: added_files
)
periphery.scan(periphery_options)
end

context 'when .swift files are not in diff' do
it 'reports nothing' do
periphery.scan(periphery_options)
expect(warnings).to be_empty
end
end
Expand All @@ -45,6 +46,7 @@
let(:added_files) { ['test/main.swift'] }

it 'reports unused code' do
periphery.scan(periphery_options)
expect(warnings).to include "Function 'unusedMethod()' is unused"
end
end
Expand All @@ -53,7 +55,16 @@
let(:modified_files) { ['test/main.swift'] }

it 'reports unused code' do
periphery.scan(periphery_options)
expect(warnings).to include "Function 'unusedMethod()' is unused"
end
end

context 'when multiple targets are analyzed' do
let(:targets) { %w[test unit-test] }

it 'does not raise any error' do
expect { periphery.scan(periphery_options) }.not_to raise_error
end
end
end
40 changes: 38 additions & 2 deletions spec/periphery/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
describe '#scan_arguments' do
subject(:scan_arguments) { runner.scan_arguments(options) }

let(:periphery_version) { '2.18.0' }

before do
status = instance_double(Process::Status, success?: true)
allow(Open3).to receive(:capture3).once.with(binary_path, 'version')
.and_return ["#{periphery_version}\n", '', status]
end

context 'with empty options' do
let(:options) { {} }

Expand Down Expand Up @@ -96,8 +104,18 @@
}
end

it 'returns correct arguments' do
expect(scan_arguments).to eq %w[--project test.xcodeproj --targets test1,test2]
context 'with Periphery >= 2.18.0' do
it 'returns space-separated arguments' do
expect(scan_arguments).to eq %w[--project test.xcodeproj --targets test1 test2]
end
end

context 'with Periphery < 2.18.0' do
let(:periphery_version) { '2.17.0' }

it 'returns comma-separated arguments' do
expect(scan_arguments).to eq %w[--project test.xcodeproj --targets test1,test2]
end
end
end

Expand Down Expand Up @@ -128,4 +146,22 @@
end
end
end

describe '#version' do
context 'when periphery succeeds' do
it 'returns the correct version' do
status = instance_double(Process::Status, success?: true)
allow(Open3).to receive(:capture3).once.with(binary_path, 'version').and_return ["2.18.0\n", '', status]
expect(runner.version).to eq '2.18.0'
end
end

context 'when periphery fails' do
it 'raises an error' do
status = instance_double(Process::Status, success?: false, exitstatus: 42)
allow(Open3).to receive(:capture3).once.with(binary_path, 'version').and_return ['', 'error', status]
expect { runner.version }.to raise_error(/error/)
end
end
end
end
115 changes: 114 additions & 1 deletion spec/support/fixtures/test.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
5B3EA09B271EE2DD00388A3A /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B3EA09A271EE2DD00388A3A /* main.swift */; };
6C2E9BD12B5E774E00133EA5 /* SomeClassTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C2E9BD02B5E774E00133EA5 /* SomeClassTests.swift */; };
6C2E9BD62B5E7A9800133EA5 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B3EA09A271EE2DD00388A3A /* main.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -25,6 +27,9 @@
/* Begin PBXFileReference section */
5B3EA097271EE2DD00388A3A /* test */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = test; sourceTree = BUILT_PRODUCTS_DIR; };
5B3EA09A271EE2DD00388A3A /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
6C2E9BCE2B5E774E00133EA5 /* unit-test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "unit-test.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
6C2E9BD02B5E774E00133EA5 /* SomeClassTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SomeClassTests.swift; sourceTree = "<group>"; };
6C2E9BD72B5E7B1600133EA5 /* fixtures */ = {isa = PBXFileReference; lastKnownFileType = folder; name = fixtures; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -35,13 +40,22 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
6C2E9BCB2B5E774E00133EA5 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
5B3EA08E271EE2DD00388A3A = {
isa = PBXGroup;
children = (
6C2E9BD72B5E7B1600133EA5 /* fixtures */,
5B3EA099271EE2DD00388A3A /* test */,
6C2E9BCF2B5E774E00133EA5 /* unit-test */,
5B3EA098271EE2DD00388A3A /* Products */,
);
sourceTree = "<group>";
Expand All @@ -50,6 +64,7 @@
isa = PBXGroup;
children = (
5B3EA097271EE2DD00388A3A /* test */,
6C2E9BCE2B5E774E00133EA5 /* unit-test.xctest */,
);
name = Products;
sourceTree = "<group>";
Expand All @@ -62,6 +77,14 @@
path = test;
sourceTree = "<group>";
};
6C2E9BCF2B5E774E00133EA5 /* unit-test */ = {
isa = PBXGroup;
children = (
6C2E9BD02B5E774E00133EA5 /* SomeClassTests.swift */,
);
path = "unit-test";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -82,18 +105,38 @@
productReference = 5B3EA097271EE2DD00388A3A /* test */;
productType = "com.apple.product-type.tool";
};
6C2E9BCD2B5E774E00133EA5 /* unit-test */ = {
isa = PBXNativeTarget;
buildConfigurationList = 6C2E9BD42B5E774E00133EA5 /* Build configuration list for PBXNativeTarget "unit-test" */;
buildPhases = (
6C2E9BCA2B5E774E00133EA5 /* Sources */,
6C2E9BCB2B5E774E00133EA5 /* Frameworks */,
6C2E9BCC2B5E774E00133EA5 /* Resources */,
);
buildRules = (
);
dependencies = (
);
name = "unit-test";
productName = "unit-test";
productReference = 6C2E9BCE2B5E774E00133EA5 /* unit-test.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
/* End PBXNativeTarget section */

/* Begin PBXProject section */
5B3EA08F271EE2DD00388A3A /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1250;
LastSwiftUpdateCheck = 1520;
LastUpgradeCheck = 1250;
TargetAttributes = {
5B3EA096271EE2DD00388A3A = {
CreatedOnToolsVersion = 12.5.1;
};
6C2E9BCD2B5E774E00133EA5 = {
CreatedOnToolsVersion = 15.2;
};
};
};
buildConfigurationList = 5B3EA092271EE2DD00388A3A /* Build configuration list for PBXProject "test" */;
Expand All @@ -110,10 +153,21 @@
projectRoot = "";
targets = (
5B3EA096271EE2DD00388A3A /* test */,
6C2E9BCD2B5E774E00133EA5 /* unit-test */,
);
};
/* End PBXProject section */

/* Begin PBXResourcesBuildPhase section */
6C2E9BCC2B5E774E00133EA5 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
5B3EA093271EE2DD00388A3A /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand All @@ -123,6 +177,15 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
6C2E9BCA2B5E774E00133EA5 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6C2E9BD62B5E7A9800133EA5 /* main.swift in Sources */,
6C2E9BD12B5E774E00133EA5 /* SomeClassTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */

/* Begin XCBuildConfiguration section */
Expand Down Expand Up @@ -259,6 +322,47 @@
};
name = Release;
};
6C2E9BD22B5E774E00133EA5 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu17;
GENERATE_INFOPLIST_FILE = YES;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MACOSX_DEPLOYMENT_TARGET = 13.5;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.github.manicmaniac.unit-test";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
6C2E9BD32B5E774E00133EA5 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu17;
GENERATE_INFOPLIST_FILE = YES;
LOCALIZATION_PREFERS_STRING_CATALOGS = YES;
MACOSX_DEPLOYMENT_TARGET = 13.5;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = "com.github.manicmaniac.unit-test";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = NO;
SWIFT_VERSION = 5.0;
};
name = Release;
};
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
Expand All @@ -280,6 +384,15 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
6C2E9BD42B5E774E00133EA5 /* Build configuration list for PBXNativeTarget "unit-test" */ = {
isa = XCConfigurationList;
buildConfigurations = (
6C2E9BD22B5E774E00133EA5 /* Debug */,
6C2E9BD32B5E774E00133EA5 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 5B3EA08F271EE2DD00388A3A /* Project object */;
Expand Down
Loading