Skip to content

Commit

Permalink
[post build lint] Check for absolute paths (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
autoantwort authored Jan 20, 2023
1 parent 7fbe87b commit fcfbfa4
Show file tree
Hide file tree
Showing 16 changed files with 425 additions and 1 deletion.
3 changes: 3 additions & 0 deletions azure-pipelines/e2e_ports/overlays/absolute-paths/hash.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

test="${CURRENT_INSTALLED_DIR}"
36 changes: 36 additions & 0 deletions azure-pipelines/e2e_ports/overlays/absolute-paths/portfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
set(VCPKG_POLICY_EMPTY_INCLUDE_FOLDER enabled)

if("usage" IN_LIST FEATURES)
file(WRITE "${CURRENT_PACKAGES_DIR}/share/${PORT}/usage" "Set TEST=${CURRENT_INSTALLED_DIR} to use the port\n")
endif()
if("header" IN_LIST FEATURES)
configure_file("${CURRENT_PORT_DIR}/source.h.in" "${CURRENT_PACKAGES_DIR}/include/test.h")
endif()
if("header-comment" IN_LIST FEATURES)
configure_file("${CURRENT_PORT_DIR}/source-comment.h.in" "${CURRENT_PACKAGES_DIR}/include/test.h")
endif()
if("python" IN_LIST FEATURES)
file(WRITE "${CURRENT_PACKAGES_DIR}/share/${PORT}/test.py" "test = \"${CURRENT_INSTALLED_DIR}\"\n")
message(STATUS "Wtite to ${CURRENT_PACKAGES_DIR}/share/${PORT}/test.py")
endif()
if("python-comment" IN_LIST FEATURES)
file(WRITE "${CURRENT_PACKAGES_DIR}/share/${PORT}/test.py" "# test = \"${CURRENT_INSTALLED_DIR}\"\n")
endif()
if("hash" IN_LIST FEATURES)
configure_file("${CURRENT_PORT_DIR}/hash.in" "${CURRENT_PACKAGES_DIR}/share/${PORT}/test")
endif()
if("new-policy" IN_LIST FEATURES)
set(VCPKG_POLICY_SKIP_ABSOLUTE_PATHS_CHECK enabled)
endif()
if("packages" IN_LIST FEATURES)
file(WRITE "${CURRENT_PACKAGES_DIR}/share/${PORT}/test.py" "${CURRENT_PACKAGES_DIR}")
endif()
if("buildtrees" IN_LIST FEATURES)
file(WRITE "${CURRENT_PACKAGES_DIR}/share/${PORT}/test.py" "${CURRENT_BUILDTREES_DIR}")
endif()
if("native" IN_LIST FEATURES)
cmake_path(NATIVE_PATH CURRENT_INSTALLED_DIR CURRENT_INSTALLED_DIR_NATIVE)
file(WRITE "${CURRENT_PACKAGES_DIR}/share/${PORT}/test.py" "${CURRENT_INSTALLED_DIR_NATIVE}")
endif()

file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/../../../../LICENSE.txt" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}" RENAME copyright)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

// char * test = "${CURRENT_INSTALLED_DIR}"

/*
${CURRENT_INSTALLED_DIR}
*/
5 changes: 5 additions & 0 deletions azure-pipelines/e2e_ports/overlays/absolute-paths/source.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

char * test2 = R"--(/*

// ${CURRENT_INSTALLED_DIR} */
)--";
37 changes: 37 additions & 0 deletions azure-pipelines/e2e_ports/overlays/absolute-paths/vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
"name": "absolute-paths",
"version": "1.0.0",
"features": {
"usage": {
"description": ""
},
"header": {
"description": ""
},
"header-comment": {
"description": ""
},
"python": {
"description": ""
},
"python-comment": {
"description": ""
},
"hash": {
"description": ""
},
"new-policy": {
"description": ""
},
"packages": {
"description": ""
},
"buildtrees": {
"description": ""
},
"native": {
"description": ""
}
}
}
41 changes: 41 additions & 0 deletions azure-pipelines/end-to-end-tests-dir/no-absolute-paths.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
. $PSScriptRoot/../end-to-end-tests-prelude.ps1

$CurrentTest = "No absolute paths"

$commonArgs += @("--enforce-port-checks", "--binarysource=clear")

Run-Vcpkg @commonArgs install "absolute-paths[hash]"
Throw-IfNotFailed
Remove-Item -Recurse -Force $installRoot

Run-Vcpkg @commonArgs install "absolute-paths[python]"
Throw-IfNotFailed
Remove-Item -Recurse -Force $installRoot

Run-Vcpkg @commonArgs install "absolute-paths[python-comment]"
Throw-IfFailed
Remove-Item -Recurse -Force $installRoot

Run-Vcpkg @commonArgs install "absolute-paths[header]"
Throw-IfNotFailed
Remove-Item -Recurse -Force $installRoot

Run-Vcpkg @commonArgs install "absolute-paths[header-comment]"
Throw-IfFailed
Remove-Item -Recurse -Force $installRoot

Run-Vcpkg @commonArgs install "absolute-paths[usage]"
Throw-IfNotFailed
Remove-Item -Recurse -Force $installRoot

Run-Vcpkg @commonArgs install "absolute-paths[usage, new-policy]"
Throw-IfFailed
Remove-Item -Recurse -Force $installRoot

Run-Vcpkg @commonArgs install "absolute-paths[packages]"
Throw-IfNotFailed
Remove-Item -Recurse -Force $installRoot

Run-Vcpkg @commonArgs install "absolute-paths[native]"
Throw-IfNotFailed
Remove-Item -Recurse -Force $installRoot
7 changes: 7 additions & 0 deletions include/vcpkg/base/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,13 @@ namespace vcpkg
"One or more {vendor} credential providers failed to authenticate. See '{url}' for more details "
"on how to provide credentials.");
DECLARE_MESSAGE(FeedbackAppreciated, (), "", "Thank you for your feedback!");
DECLARE_MESSAGE(
FilesContainAbsolutePath1,
(),
"This message is printed before a list of found absolute paths, followed by FilesContainAbsolutePath2, "
"followed by a list of found files.",
"There should be no absolute paths, such as the following, in an installed package:");
DECLARE_MESSAGE(FilesContainAbsolutePath2, (), "", "Absolute paths were found in the following files:");
DECLARE_MESSAGE(FetchingBaselineInfo,
(msg::package_name),
"",
Expand Down
8 changes: 8 additions & 0 deletions include/vcpkg/base/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <algorithm>
#include <vector>

#include "vcpkg/base/fwd/span.h"

namespace vcpkg::Strings::details
{
// first looks up to_string on `T` using ADL; then, if that isn't found,
Expand Down Expand Up @@ -218,6 +220,12 @@ namespace vcpkg::Strings

Optional<StringView> find_at_most_one_enclosed(StringView input, StringView left_tag, StringView right_tag);

bool contains_any_ignoring_c_comments(const std::string& source, View<StringView> to_find);

bool contains_any_ignoring_hash_comments(StringView source, View<StringView> to_find);

bool contains_any(StringView source, View<StringView> to_find);

bool equals(StringView a, StringView b);

template<class T>
Expand Down
7 changes: 7 additions & 0 deletions include/vcpkg/base/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ namespace vcpkg::Util
return std::find(begin(cont), end(cont), v);
}

template<class Range, class T>
bool contains(const Range& r, const T& el)
{
using std::end;
return Util::find(r, el) != end(r);
}

template<class Container, class Pred>
auto find_if(Container&& cont, Pred pred)
{
Expand Down
1 change: 1 addition & 0 deletions include/vcpkg/fwd/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace vcpkg
SKIP_DUMPBIN_CHECKS,
SKIP_ARCHITECTURE_CHECK,
CMAKE_HELPER_PORT,
SKIP_ABSOLUTE_PATHS_CHECK,
// Must be last
COUNT,
};
Expand Down
3 changes: 3 additions & 0 deletions locales/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@
"FileSeekFailed": "Failed to seek to position {byte_offset} in {path}.",
"_FileSeekFailed.comment": "An example of {path} is /foo/bar. An example of {byte_offset} is 42.",
"FileSystemOperationFailed": "Filesystem operation failed:",
"FilesContainAbsolutePath1": "There should be no absolute paths, such as the following, in an installed package:",
"_FilesContainAbsolutePath1.comment": "This message is printed before a list of found absolute paths, followed by FilesContainAbsolutePath2, followed by a list of found files.",
"FilesContainAbsolutePath2": "Absolute paths were found in the following files:",
"FilesExported": "Files exported at: {path}",
"_FilesExported.comment": "An example of {path} is /foo/bar.",
"FishCompletion": "vcpkg fish completion is already added at \"{path}\".",
Expand Down
70 changes: 70 additions & 0 deletions src/vcpkg-test/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <vcpkg/base/api-stable-format.h>
#include <vcpkg/base/expected.h>
#include <vcpkg/base/span.h>
#include <vcpkg/base/strings.h>

#include <stdint.h>
Expand Down Expand Up @@ -55,6 +56,75 @@ TEST_CASE ("find_first_of", "[strings]")
REQUIRE(find_first_of("abcdefg", "gb") == std::string("bcdefg"));
}

TEST_CASE ("contains_any_ignoring_c_comments", "[strings]")
{
using vcpkg::Strings::contains_any_ignoring_c_comments;
vcpkg::StringView to_find[] = {"abc", "wer"};
REQUIRE(contains_any_ignoring_c_comments(R"(abc)", to_find));
REQUIRE(contains_any_ignoring_c_comments(R"("abc")", to_find));
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"("" //abc)", to_find));
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"(/*abc*/ "")", to_find));
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"(/**abc*/ "")", to_find));
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"(/**abc**/ "")", to_find));
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"(/*abc)", to_find));
// note that the line end is escaped making the single line comment include the abc
REQUIRE_FALSE(contains_any_ignoring_c_comments("// test \\\nabc", to_find));
// note that the comment start is in a string literal so it isn't a comment
REQUIRE(contains_any_ignoring_c_comments("\"//\" test abc", to_find));
// note that the comment is in a raw string literal so it isn't a comment
REQUIRE(contains_any_ignoring_c_comments(R"-(R"( // abc )")-", to_find));
// found after the raw string literal
REQUIRE(contains_any_ignoring_c_comments(R"-(R"( // )" abc)-", to_find));
// comment after the raw string literal
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"-(R"( // )" // abc)-", to_find));
// the above, but with a d_char_sequence for the raw literal
REQUIRE(contains_any_ignoring_c_comments(R"-(R"hello( // abc )hello")-", to_find));
REQUIRE(contains_any_ignoring_c_comments(R"-(R"hello( // )hello" abc)-", to_find));
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"-(R"hello( // )hello" // abc)-", to_find));
// the above, but with a d_char_sequence that is a needle
REQUIRE(contains_any_ignoring_c_comments(R"-(R"abc( // abc )abc")-", to_find));
REQUIRE(contains_any_ignoring_c_comments(R"-(R"abc( // )abc" abc)-", to_find));
REQUIRE(contains_any_ignoring_c_comments(R"-(R"abc( // )abc" // abc)-", to_find));
// raw literal termination edge cases
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"-(R")-", to_find)); // ends input
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"-(R"h)-", to_find)); // ends input d_char
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"-(R"()-", to_find)); // ends input paren
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"-(R"h()-", to_find)); // ends input paren d_char
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"-(R"())-", to_find)); // ends input close paren
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"-(R"()")-", to_find)); // ends input exactly
// raw literal termination edge cases (success)
REQUIRE(contains_any_ignoring_c_comments(R"-(abcR")-", to_find)); // ends input
REQUIRE(contains_any_ignoring_c_comments(R"-(abcR"h)-", to_find)); // ends input d_char
REQUIRE(contains_any_ignoring_c_comments(R"-(abcR"()-", to_find)); // ends input paren
REQUIRE(contains_any_ignoring_c_comments(R"-(abcR"h()-", to_find)); // ends input paren d_char
REQUIRE(contains_any_ignoring_c_comments(R"-(abcR"())-", to_find)); // ends input close paren
REQUIRE(contains_any_ignoring_c_comments(R"-(abcR"()")-", to_find)); // ends input exactly

REQUIRE(contains_any_ignoring_c_comments(R"-(R"()"abc)-", to_find));

REQUIRE(contains_any_ignoring_c_comments(R"-(R"hello( hello" // abc )")-", to_find));
REQUIRE(contains_any_ignoring_c_comments(R"(R"-( // abc )-")", to_find));
REQUIRE_FALSE(contains_any_ignoring_c_comments(R"(R"-( // hello )-" // abc)", to_find));
REQUIRE(contains_any_ignoring_c_comments(R"(R"-( /* abc */ )-")", to_find));
REQUIRE(contains_any_ignoring_c_comments(R"(R"-()- /* abc */ )-")", to_find));
REQUIRE(contains_any_ignoring_c_comments(R"(qwer )", to_find));
REQUIRE(contains_any_ignoring_c_comments("\"a\" \"g\" // er \n abc)", to_find));
}

TEST_CASE ("contains_any_ignoring_hash_comments", "[strings]")
{
using vcpkg::Strings::contains_any_ignoring_hash_comments;
vcpkg::StringView to_find[] = {"abc", "wer"};
REQUIRE(contains_any_ignoring_hash_comments("abc", to_find));
REQUIRE(contains_any_ignoring_hash_comments("wer", to_find));
REQUIRE(contains_any_ignoring_hash_comments("wer # test", to_find));
REQUIRE(contains_any_ignoring_hash_comments("\n wer # \n test", to_find));
REQUIRE_FALSE(contains_any_ignoring_hash_comments("# wer", to_find));
REQUIRE_FALSE(contains_any_ignoring_hash_comments("\n# wer", to_find));
REQUIRE_FALSE(contains_any_ignoring_hash_comments("\n # wer\n", to_find));
REQUIRE_FALSE(contains_any_ignoring_hash_comments("\n test # wer", to_find));
}

TEST_CASE ("edit distance", "[strings]")
{
using vcpkg::Strings::byte_edit_distance;
Expand Down
4 changes: 3 additions & 1 deletion src/vcpkg/base/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,13 +659,15 @@ namespace vcpkg
REGISTER_MESSAGE(FeedbackAppreciated);
REGISTER_MESSAGE(FetchingBaselineInfo);
REGISTER_MESSAGE(FetchingRegistryInfo);
REGISTER_MESSAGE(FishCompletion);
REGISTER_MESSAGE(FloatingPointConstTooBig);
REGISTER_MESSAGE(FileNotFound);
REGISTER_MESSAGE(FileReadFailed);
REGISTER_MESSAGE(FileSeekFailed);
REGISTER_MESSAGE(FilesExported);
REGISTER_MESSAGE(FileSystemOperationFailed);
REGISTER_MESSAGE(FishCompletion);
REGISTER_MESSAGE(FilesContainAbsolutePath1);
REGISTER_MESSAGE(FilesContainAbsolutePath2);
REGISTER_MESSAGE(FollowingPackagesMissingControl);
REGISTER_MESSAGE(FollowingPackagesNotInstalled);
REGISTER_MESSAGE(FollowingPackagesUpgraded);
Expand Down
Loading

0 comments on commit fcfbfa4

Please sign in to comment.