Skip to content

Commit

Permalink
Limit snapshotId length
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 committed Jan 25, 2024
1 parent 6fb1391 commit 155d263
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion spock-core/src/main/java/spock/lang/Snapshotter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 the original author or authors.
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import org.spockframework.runtime.model.IterationInfo;
import org.spockframework.runtime.model.TextPosition;
import org.spockframework.util.Beta;
import org.spockframework.util.Checks;
import org.spockframework.util.IoUtil;

import java.io.IOException;
Expand Down Expand Up @@ -246,6 +247,9 @@ public Store(IterationInfo iterationInfo, Path rootPath, boolean updateSnapshots
}

private static String calculateSafeUniqueName(String extension, IterationInfo iterationInfo, String snapshotId) {
Checks.checkArgument(snapshotId.length() <= 100,
() -> String.format("'snapshotId' is too long, only 100 characters are allowed, but was %d: %s", snapshotId.length(), snapshotId));

FeatureInfo feature = iterationInfo.getFeature();
String safeName = sanitize(feature.getName());
String featureId = feature.getFeatureMethod().getReflection().getName().substring("$spock_feature_".length());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 the original author or authors.
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,9 +78,19 @@ class SnapshotterSpec extends Specification {
featureName | iterationIndex | extension | snapshotId | safeName
'a feature' | 0 | 'txt' | 'a snapshot' | 'a_feature-a_snapshot.txt'
'a feature' | 0 | 'txt' | '' | 'a_feature.txt'
'a feature' | 0 | 'groovy' | '' | 'a_feature.groovy'
'a feature' | 1 | 'txt' | 'a snapshot' | 'a_feature-a_snapshot-[1].txt'
'a feature' | 1 | 'txt' | '' | 'a_feature-[1].txt'
'a' * 300 | 0 | 'txt' | '' | 'a' * 242 + '-0_1.txt'
'a' * 300 | 100 | 'txt' | 'a longer snapshot id' | 'a' * 215 + '-a_longer_snapshot_id-0_1-[100].txt'
}
def "snapshotId is limited to 100 characters"() {
when:
Snapshotter.Store.calculateSafeUniqueName('txt', Stub(IterationInfo), "a" * 101)
then:
IllegalArgumentException e = thrown()
e.message.startsWith("'snapshotId' is too long, only 100 characters are allowed, but was 101: aaaaa")
}
}

0 comments on commit 155d263

Please sign in to comment.