Skip to content
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
22 changes: 22 additions & 0 deletions .github/workflows/xdr-generator-snapshot-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: XDR Generator Snapshot Test

on:
push:
branches: [master]
paths:
- "xdr-generator/**"
- "Makefile"
- ".github/workflows/xdr-generator-snapshot-test.yml"
pull_request:
paths:
- "xdr-generator/**"
- "Makefile"
- ".github/workflows/xdr-generator-snapshot-test.yml"

jobs:
snapshot-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run XDR generator snapshot tests
run: make xdr-generator-test
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ xdr/Stellar-exporter.x
# stellar-xdr commit to use, see https://github.com/stellar/stellar-xdr
XDR_COMMIT=4b7a2ef7931ab2ca2499be68d849f38190b443ca

.PHONY: xdr xdr-clean xdr-update
.PHONY: xdr xdr-clean xdr-update xdr-generator-test xdr-generator-update-snapshots

xdr-generate: $(XDRS)
docker run --rm -v $$PWD:/wd -w /wd ruby:3.4 /bin/bash -c '\
Expand All @@ -33,3 +33,15 @@ xdr-clean:
find src/main/java/org/stellar/sdk/xdr -type f -name "*.java" ! -name "package-info.java" -delete

xdr-update: xdr-clean xdr-generate

xdr-generator-test:
docker run --rm -v $$PWD:/wd -w /wd ruby:3.4 /bin/bash -c '\
cd xdr-generator && \
bundle install --quiet && \
bundle exec ruby test/generator_snapshot_test.rb'

xdr-generator-update-snapshots:
docker run --rm -v $$PWD:/wd -w /wd ruby:3.4 /bin/bash -c '\
cd xdr-generator && \
bundle install --quiet && \
UPDATE_SNAPSHOTS=1 bundle exec ruby test/generator_snapshot_test.rb'
2 changes: 2 additions & 0 deletions xdr-generator/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ gem "benchmark"
gem "bigdecimal"
gem "logger"
gem "mutex_m"

gem "minitest"
1 change: 1 addition & 0 deletions xdr-generator/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ DEPENDENCIES
benchmark
bigdecimal
logger
minitest
mutex_m
xdrgen!

Expand Down
13 changes: 13 additions & 0 deletions xdr-generator/test/fixtures/xdrgen/block_comments.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum AccountFlags
{ // masks for each flag
AUTH_REQUIRED_FLAG = 0x1
};

/* AccountEntry

Main entry representing a user in Stellar. All transactions are performed
using an account.

Other ledger entries created require an account.

*/
4 changes: 4 additions & 0 deletions xdr-generator/test/fixtures/xdrgen/const.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const FOO = 1;

typedef int TestArray[FOO];
typedef int TestArray2<FOO>;
42 changes: 42 additions & 0 deletions xdr-generator/test/fixtures/xdrgen/enum.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
enum MessageType
{
ERROR_MSG,
HELLO,
DONT_HAVE,

GET_PEERS, // gets a list of peers this guy knows about
PEERS,

GET_TX_SET, // gets a particular txset by hash
TX_SET,

GET_VALIDATIONS, // gets validations for a given ledger hash
VALIDATIONS,

TRANSACTION, //pass on a tx you have heard about
JSON_TRANSACTION,

// FBA
GET_FBA_QUORUMSET,
FBA_QUORUMSET,
FBA_MESSAGE
};

enum Color {
RED=0,
GREEN=1,
BLUE=2
};


enum Color2 {
RED2=RED,
GREEN2=1,
BLUE2=2
};

enum Color3 {
RED_1=1,
RED_2_TWO=2,
RED_3=3
};
24 changes: 24 additions & 0 deletions xdr-generator/test/fixtures/xdrgen/nesting.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
enum UnionKey {
ONE = 1,
TWO = 2,
OFFER = 3
};

typedef int Foo;

union MyUnion switch (UnionKey type)
{
case ONE:
struct {
int someInt;
} one;

case TWO:
struct {
int someInt;
Foo foo;
} two;

case OFFER:
void;
};
8 changes: 8 additions & 0 deletions xdr-generator/test/fixtures/xdrgen/optional.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
typedef int Arr[2];

struct HasOptions
{
int* firstOption;
int *secondOption;
Arr *thirdOption;
};
10 changes: 10 additions & 0 deletions xdr-generator/test/fixtures/xdrgen/struct.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
typedef hyper int64;

struct MyStruct
{
int someInt;
int64 aBigInt;
opaque someOpaque[10];
string someString<>;
string maxString<100>;
};
77 changes: 77 additions & 0 deletions xdr-generator/test/fixtures/xdrgen/test.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
%#include "generated/FBAXDR.h"

namespace MyNamespace {

// messages
typedef opaque uint512[64];
typedef opaque uint513<64>;
typedef opaque uint514<>;
typedef string str<64>;
typedef string str2<>;

typedef opaque Hash[32];
typedef Hash Hashes1[12];
typedef Hash Hashes2<12>;
typedef Hash Hashes3<>;


typedef Hash *optHash1;
typedef Hash* optHash2;

typedef int int1;
typedef hyper int2;
typedef unsigned int int3;
typedef unsigned hyper int4;

struct MyStruct
{
uint512 field1;
optHash1 field2;
int1 field3;
unsigned int field4;
float field5;
double field6;
bool field7;
};

struct LotsOfMyStructs
{
MyStruct members<>;
};

struct HasStuff
{
LotsOfMyStructs data;
};

enum Color {
RED,
BLUE = 5,
GREEN
};

const FOO = 1244;
const BAR = FOO;

struct Nester
{
enum {
BLAH_1,
BLAH_2
} nestedEnum;

struct {
int blah;
} nestedStruct;

union switch (Color color) {
case RED:
void;
default:
int blah2;
} nestedUnion;


};

}
28 changes: 28 additions & 0 deletions xdr-generator/test/fixtures/xdrgen/union.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
typedef int Error;
typedef int Multi;

enum UnionKey {
ERROR,
MULTI
};

union MyUnion switch (UnionKey type)
{
case ERROR:
Error error;
case MULTI:
Multi things<>;


};

union IntUnion switch (int type)
{
case 0:
Error error;
case 1:
Multi things<>;

};

typedef IntUnion IntUnion2;
89 changes: 89 additions & 0 deletions xdr-generator/test/generator_snapshot_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require "fileutils"
require "minitest/autorun"
require "tmpdir"

require "xdrgen"
require_relative "../generator/generator"

class GeneratorSnapshotTest < Minitest::Test
FIXTURES_DIR = File.expand_path("fixtures/xdrgen", __dir__)
SNAPSHOTS_DIR = File.expand_path("snapshots", __dir__)
UPDATE_SNAPSHOTS = ENV["UPDATE_SNAPSHOTS"] == "1"

def test_fixture_snapshots
fixtures = fixture_paths
refute_empty fixtures, "No fixture files were found in #{FIXTURES_DIR}"

fixtures.each do |fixture_path|
assert_fixture_snapshot(fixture_path)
end
end

private

def fixture_paths
Dir[File.join(FIXTURES_DIR, "*.x")].sort
end

def assert_fixture_snapshot(fixture_path)
fixture_name = File.basename(fixture_path, ".x")
snapshot_dir = File.join(SNAPSHOTS_DIR, fixture_name)

Dir.mktmpdir("xdrgen-#{fixture_name}-") do |tmp_dir|
generated_dir = File.join(tmp_dir, "generated")
compile_fixture(fixture_path, generated_dir)

if UPDATE_SNAPSHOTS
FileUtils.rm_rf(snapshot_dir)
FileUtils.mkdir_p(snapshot_dir)
FileUtils.cp_r("#{generated_dir}/.", snapshot_dir)
next
end

assert File.directory?(snapshot_dir), <<~MSG
Missing snapshot for #{fixture_name}.
Run UPDATE_SNAPSHOTS=1 bundle exec ruby test/generator_snapshot_test.rb
MSG

assert_files_match(snapshot_dir, generated_dir, fixture_name)
end
end

def compile_fixture(fixture_path, output_dir)
FileUtils.mkdir_p(output_dir)
Xdrgen::Compilation.new(
[fixture_path],
output_dir: output_dir,
generator: Generator,
namespace: "org.stellar.sdk.xdr",
).compile
end

def assert_files_match(expected_dir, actual_dir, fixture_name)
expected_files = collect_relative_files(expected_dir)
actual_files = collect_relative_files(actual_dir)

assert_equal expected_files, actual_files, <<~MSG
Generated file list changed for fixture #{fixture_name}.
Expected: #{expected_files.join(", ")}
Actual: #{actual_files.join(", ")}
MSG

expected_files.each do |relative_path|
expected_path = File.join(expected_dir, relative_path)
actual_path = File.join(actual_dir, relative_path)
expected_content = File.binread(expected_path)
actual_content = File.binread(actual_path)

assert_equal expected_content, actual_content, <<~MSG
Generated content changed for fixture #{fixture_name}: #{relative_path}
MSG
end
end

def collect_relative_files(root_dir)
Dir.chdir(root_dir) do
Dir.glob("**/*", File::FNM_DOTMATCH).sort.select { |path| File.file?(path) }
end
end
end
Loading
Loading