Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3d2fb855 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "AAPT2: Update ReplacePlaceholder for artifact name parser"

parents ed9c1197 1a21b8c0
Loading
Loading
Loading
Loading
+18 −6
Original line number Diff line number Diff line
@@ -118,21 +118,33 @@ const std::string& AbiToString(Abi abi) {
static bool ReplacePlaceholder(const std::string& placeholder, const Maybe<std::string>& value,
                               std::string* name, IDiagnostics* diag) {
  size_t offset = name->find(placeholder);
  bool found = (offset != std::string::npos);

  // Make sure the placeholder was present if the desired value is present.
  if (!found) {
    if (value) {
    if (offset == std::string::npos) {
      diag->Error(DiagMessage() << "Missing placeholder for artifact: " << placeholder);
      return false;
    }
    name->replace(offset, placeholder.length(), value.value());
    return true;
  }

  DCHECK(found) << "Missing return path for placeholder not found";

  // Make sure the placeholder was not present if the desired value was not present.
  bool result = (offset == std::string::npos);
  if (!result) {
  if (!value) {
    diag->Error(DiagMessage() << "Placeholder present but no value for artifact: " << placeholder);
    return false;
  }
  return result;

  name->replace(offset, placeholder.length(), value.value());

  // Make sure there was only one instance of the placeholder.
  if (name->find(placeholder) != std::string::npos) {
    diag->Error(DiagMessage() << "Placeholder present multiple times: " << placeholder);
    return false;
  }
  return true;
}

Maybe<std::string> Artifact::ToArtifactName(const std::string& format, IDiagnostics* diag) const {
+50 −0
Original line number Diff line number Diff line
@@ -418,6 +418,8 @@ TEST_F(ConfigurationParserTest, DeviceFeatureGroupAction) {
  ASSERT_THAT(out, ElementsAre(low_latency, pro));
}

// Artifact name parser test cases.

TEST(ArtifactTest, Simple) {
  StdErrDiagnostics diag;
  Artifact x86;
@@ -468,5 +470,53 @@ TEST(ArtifactTest, Empty) {
  EXPECT_TRUE(artifact.ToArtifactName("something.apk", &diag));
}

TEST(ArtifactTest, Repeated) {
  StdErrDiagnostics diag;
  Artifact artifact;
  artifact.screen_density_group = {"mdpi"};

  EXPECT_TRUE(artifact.ToArtifactName("something.{density}.apk", &diag));
  EXPECT_FALSE(artifact.ToArtifactName("something.{density}.{density}.apk", &diag));
}

TEST(ArtifactTest, Nesting) {
  StdErrDiagnostics diag;
  Artifact x86;
  x86.abi_group = {"x86"};

  EXPECT_FALSE(x86.ToArtifactName("something.{abi{density}}.apk", &diag));

  const Maybe<std::string>& name = x86.ToArtifactName("something.{abi{abi}}.apk", &diag);
  EXPECT_TRUE(name);
  EXPECT_EQ(name.value(), "something.{abix86}.apk");
}

TEST(ArtifactTest, Recursive) {
  StdErrDiagnostics diag;
  Artifact artifact;
  artifact.device_feature_group = {"{gl}"};
  artifact.gl_texture_group = {"glx1"};

  EXPECT_FALSE(artifact.ToArtifactName("app.{feature}.{gl}.apk", &diag));

  artifact.device_feature_group = {"df1"};
  artifact.gl_texture_group = {"{feature}"};
  {
    const auto& result = artifact.ToArtifactName("app.{feature}.{gl}.apk", &diag);
    EXPECT_TRUE(result);
    EXPECT_EQ(result.value(), "app.df1.{feature}.apk");
  }

  // This is an invalid case, but should be the only possible case due to the ordering of
  // replacement.
  artifact.device_feature_group = {"{gl}"};
  artifact.gl_texture_group = {"glx1"};
  {
    const auto& result = artifact.ToArtifactName("app.{feature}.apk", &diag);
    EXPECT_TRUE(result);
    EXPECT_EQ(result.value(), "app.glx1.apk");
  }
}

}  // namespace
}  // namespace aapt