Disable some SwiftLint rules for test target/unit tests files - swift

I am looking to disable a couple of SwiftLint rules for the unit tests within my application.
For example I am wanting to disable the weak_delegate rule for my unit tests.
Having looked at the SwiftLint docs I think it may be possible by defining a custom weak_delegate rule and excluding the path to my unit tests.
https://github.com/realm/SwiftLint#defining-custom-rules

You can disable them at a local level using:
//swiftlint:disable weak_delegate
let someDelete: someDelegate?
//swiftlint:enable weak_delegate
or at target level, by modifying your .swiftlint.yml file (hidden)
weak_delegate:
excluded: ".*Test\\.swift" //regex path to your tests folder
or at project level, by modifying your .swiftlint.yml file (hidden)
disabled_rules:
- weak_delegate

Best way to exclude some rules for test target is the nested configuration: you add a second .swiftlint.yml to root of your tests directory with rules to disable.
disabled_rules:
- weak_delegate
- cyclomatic_complexity
- force_unwrapping
- function_body_length

Add this to your .swiftlint.yml:
weak_delegate:
excluded: ".*Test\\.swift" //regex path to your tests folder

Change the script in Build Phases to point only to the folder of your source code:
if which swiftlint > /dev/null; then
swiftlint --path "$SRCROOT"/[name_of_source_folder]
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
Adding the flag --path "$SRCROOT"/path_to_folder will exclude the test targets.

Related

CMake under eclipse - locate linked files and libraries from other non-cmake projects

I got a following problem. We have a very silly git structure and guidelines, so my eclipse projects use linked files. I have to make google tests for the projects, and the only way I found it to work is to use cmake. However, the test files are not in workspace but in a git repository and only linked to eclipse project. I cant figure out how to make CMakeLists.txt to follow the files.... I tried using include_directories with either relative or absolute paths..
Errors I get:
mingw32-make.exe[2]: *** No rule to make target 'C:/Users/name/eclipse-
workspace/MyTestCmake/Solver_test.cpp', needed by 'CMakeFiles/Solver_test.dir/Solver_test.cpp.obj'. Stop.
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:164: CMakeFiles/Solver_test.dir/all] Error 2
mingw32-make.exe: *** [Makefile:145: all] Error 2
Another question is - one of the libraries is built without cmake. How can I specify the path to it, where the cmake should find it?
Current Cmakelists.txt:
cmake_minimum_required(VERSION 3.14)
project (RadiTestCmake)
include(FetchContent)
FetchContent_Declare(googletest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG release-1.12.1 )
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
include_directories(
"${PROJECT_BINARY_DIR}"
"${PROJECT_BINARY_DIR}/../../../../../../gitlocal/backend/tests/"
)
add_executable(
hello_test
hello_test.cc
Solver_test.cpp
)
target_link_libraries(
hello_test
GTest::gtest_main
)
include(GoogleTest)
gtest_discover_tests(hello_test)

SwiftLint 'excluded' not working for one folder

my swiftlint.yml file is not excluding my "Generated Mocks" folder. I've tried Generated\ Mocks and 'Generated Mocks' Is there a solution to this?
excluded:
- Pods
- MyProjectTests
- MyProject/Helpers/Constants.swift
- Generated Mocks
edit: The Generated Mocks folder was generated from a dependency I added so I hope that helps
Instead of writing excluded commands in swiftlint.yml file, simply use included command and add your project path, tests etc. in here (paths where you want your swiftlint to run).
For instance, my swiftlint.yml file looks like this:
included:
- [PROJECT_NAME]
- [PROJECT_NAME]Tests
- [PROJECT_NAME]UITests
I only have a included command and I added paths where I want my swiftlint rules to work.
Make sure to add this command:
if which swiftlint >/dev/null; then
swiftlint --config [PROJECT_PATH]/.swiftlint.yml
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
to:
Target > Build Phases > Run Script
And lastly, make sure that your .swiftlint.yml file is located of your project's root path.
Build the project, you'll see that your SwiftLint warnings will show only for your included folders & files.
Another approach would be to change the destination folder name from Genera†ed Mocks to GeneratedMocks, and then use this in .yaml (works for me):
excluded:
- Pods
- UnitTests/generated/GeneratedMocks.swift

No lintable files found at paths SwiftLint

I tried to install SwiftLint using CocoaPods and I add in Build phases the following script :
"${PODS_ROOT}/SwiftLint/swiftlint"
SwiftLint is installed correctly and I get many errors and warnings in the project.
Then, I create the swiftLint.yml file in which I modify some rules but they are not token into consideration and the same number of errors and warnings persist in Xcode project.
When I run this command to confirm the application of the rules :
./swiftlint lint --config .swiftlint.yml
I get the error :
No lintable files found at paths : ''
How can I solve this issue please?
It happens also if you rename the directory of your app, make sure you report the change in the .swiftlint.yml too at first lines :
included: # paths to include during linting
- My_App_Directory
For those of you who used 0.42.0 before and updated to 0.43.0 (or higher?).
They made a change and now interpret included and excluded as relative paths.
Configuration files now consistently have their included/excluded
relative file paths applied relative to their location in the file
system. Previously the root configuration file applied these relative
to the current working directory, but nested configurations applied
these to their location in the file system.
From the release notes of 0.43.0: Clothes Line Interface.
if you are using swiftLint with CocoaPods : try "${PODS_ROOT}/SwiftLint/swiftlint" --config .swiflint.yml in your SwiftLint Run Script in your project build phases.
make sure your .swiflint.yml config file is in the root of your project directory ( beside .xcodeproj file ).
make sure the paths included on your .swiflint.yml (in included: and excluded: sections ) is valid paths
make sure your .swiflint.yml file is valid yaml
don't escape the directory paths in your config file
dont do : - some\ Directorybut do - some Directory without escape character.
If you installed it using Cocoapods this can help you.
I will just merely improve the above answers, to put clarity on how to resolve the issue of SwiftLint not finding the path.
Things to lookout for.
Make sure your swiftlint.yml file is valid.
Make sure the swiftlint.yml is in the same level as your .xcodeproj
Don't specify --path and also add an entry under included: inside your yml file, choose one, either specify the --path or add an entry don't use both otherwise SwiftLint will ignore the --path param, and if the entry specified inside included: is wrong you will get the "no lintable file found" error
In your script.
The gihub page of SwiftLint recommends just using "${PODS_ROOT}/SwiftLint/swiftlint" but that didn't work for me, I had to specify the --path see below for the full script.
"${PODS_ROOT}/SwiftLint/swiftlint" --path "${SRCROOT}/Classes" --config "directory-of-your-config"
The --path param should be your own --path "${SRCROOT}/Classes"
Finally inside the yml file.
Make sure your included and excluded file specification is correct, see how I did mine below.
included:
- Your-Project-Name
excluded:
- Pods
One Important thing to note is if you add directories under included: the --path param will be ignored, and you might possibly get the "no lintable files found" error if the directory is wrong.
First of all, you do not need to add explicitly the--config file if the yml file is in the running directory (from where you are running the command) and name is .swiftlint.yml.
Secondly, you need to specify the path for your source with --path. Below command will work fine in your case,
swiftlint lint --path SourcePath
For swiftlint from version 0.41 the following code worked for me in the build phase (workspace with several projects. Depending on the project configuration, it may be that "../" has to be removed from the path information):
cd ${PROJECT_DIR}/../
"${PODS_ROOT}/SwiftLint/swiftlint" --config "${PROJECT_DIR}/../.swiftlint.yml"
Here is a screenshot of the build phase entry:
Replace autocorrect with --fix
export PATH="$PATH:/opt/homebrew/bin" //This line is only for Apple Silicon chips
if which swiftlint > /dev/null; then
swiftlint --fix && swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
For M1, don't go for swift lint installation VIA PODS instead use Brew.
For installation run below command in Terminal
brew install swiftlint
and add below scripts into RunScript into build phase of your target.
export PATH="$PATH:/opt/homebrew/bin"
if which swiftlint > /dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
If you using pod file then follow this steps:
Run Script: "${PODS_ROOT}/SwiftLint/swiftlint"
This worked for me
if which "${PODS_ROOT}/SwiftLint/swiftlint" >/dev/null; then
${PODS_ROOT}/SwiftLint/swiftlint --fix && ${PODS_ROOT}/SwiftLint/swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

Custom deployment with sbt

I have a Scala application and i want to setup a deployment process result similar to one Akka sbt plugin gives, but i it requires project to be a microkernel. Sbt-assembly is a very cool plugin, but i want to have a free access to my config files, basically i want to have the following app structure:
/app/bin - start script bash file
/config - all my application .conf files
/deploy - my project .jar with classes
/lib - all my dependencies .jar files
/logs - log files
I we checked typesafe sbt-native-packager, it's better, but it could place my .conf file and logs out of jars. All those settings in SBT looks confusing to me, what can i do to with this?
Actually this is not hard to update akka-sbt-plugin to make it work in general, add this file to your project folder and somewhere in your build the following settings:
.settings(Distribution.distSettings: _*)
.settings(mappings in Compile in packageBin ~= { _.filter(!_._1.getName.endsWith(".conf")) })
The first line adds all dist settings to your project and the second one excludes all .conf files from your .jar and reads them from config folder.
Now you have to commands: dist - creates a folder with a structure you've discribed and zipDist which packs it all into .zip file, later you can add this to you publish setting:
addArtifact(Artifact(someName, "zip", "zip"), zipDist)

How do I add a make target to a build configuration in Eclipse?

I've added a test target to makefile.targets in an Eclipse C++ project. Now, I want it to be built as part of my Debug and Release build configurations so I can have my unit tests run as part of the normal build process.
How do I do this, given that I cannot edit the auto-generated Debug/makefile and Release/makefile?
The pseudo-code below hopefully answers the question about target addition, and additionally addresses how to use variables in the makefile and source code.
It took me a day to figure this out using web resources, especially stackoverflow.com and eclipse.org forums. The Eclipse documentation for CDT is a bit vague on some points.
// pseudo-code for Eclipse version Kepler
if ("Project.Properties.C/C++ Build.Generate Makefiles automatically" == true) {
// when using the automatically generated makefiles,
// use the -include's in the generated Debug/makefile
cp <your makefile init statements> $(ProjDirPath)/makefile.init
cp <your makefile definitions> $(ProjDirPath)/makefile.defs
cp <your makefile targets> $(ProjDirPath)/makefile.targets
} else {
// when using a makefile that you maintain, alter your own makefile
cp <your makefile targets> <your makefile>
}
// Additionally, you may want to provide variables for use in the makefile
// commands, whether it's your own makefile or the generated one:
// Note that:
// - "Preferences.C/C++.Build.Build Variables" and
// "Project.Properties.C/C++ Build.Build Variables"
// are *NOT directly available* to the makefile commands.
// - "Preferences.C/C++.Build.Environment" variables and
// "Project.Properties.C/C++ Build.Environment" variables
// *ARE available* to the makefile commands.
// - To make "Build Variables" available to the makefile and source files,
// add an environment variable as shown below. Especially useful are the
// built-in ones visible when "Show system variables" is checked.
// assign the build system variable "ProjDirPath" as a *user preference*
"Preferences.C/C++.Build.Environment".AddKeyValue("ProjDirPath",
"${ProjDirPath}"}
// assign the build system variable "ProjDirPath" as a *project property*
"Project.Properties.C/C++ Build.Environment".AddKeyValue("ProjDirPath",
${ProjDirPath}")
An example "makefile.init":
GREP := /bin/grep
An example "makefile.defs":
ifndef ProjDirPath
$(error "ProjDirPath" undefined as a "make variable" or "environment variable".)
endif
The generated makefile Debug/src/subdir.mk extracts dependencies into
the file ${ProjDirPath}/Debug/src/${ProjName}.d, but you need to add an extra target for initial generation of a dependency. You could add a target for an:
#include "automated_headers.h"
by adding targets to ${ProjDirPath}/makefile.targets.
An example "makefile.targets":
# targets to generate a header file
%/src/automated_headers.h: %/src/generate_headers.py $(external_library_info)
#echo "Generating $#"
%/src/generate_headers.py $(extrnal_library_info) $#
src/$(ProjName).o: $(ProjDirPath)/src/automated_headers.h
Open the Make Target view if it's not already open. It has a New Make Target button.
Alternatively, highlight the name of the target in the makefile, right click, and go to Make Targets -> Create....
Edit: I may have misunderstood your question. If you want the target to be built, when you click build, go to the build preferences and add it there.