Skip to content

Commit

Permalink
Add version set support.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodov committed Jul 11, 2024
1 parent f1e866c commit a06d532
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/generate-test-seed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
env:
ACTION_RUN_URL: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}'
REMOTE_SEED_PATH: 'pull/${{ github.event.pull_request.number }}/seed'
SEED_VERSION: 'pull/${{ github.event.pull_request.number }}@${{ github.event.pull_request.head.sha }}'

steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
Expand Down Expand Up @@ -63,9 +64,9 @@ jobs:
- name: Generate seed
run: |
# Use only python implementation for now.
python seed/serialize.py seed/seed.json
python seed/serialize.py seed/seed.json --version "$SEED_VERSION"
# TODO: enable this when per-file studies will be synced with seed.json.
# npm run seed_tools -- create_seed studies seed.bin
# npm run seed_tools -- create_seed studies seed.bin --version "$SEED_VERSION"
- name: Upload seed
env:
Expand Down Expand Up @@ -97,8 +98,9 @@ jobs:
\`\`\`
#### Seed Details
- Serial Number: \`${serialNumberContent}\`
- Version: \`${process.env.SEED_VERSION}\`
- Uploaded: \`${new Date().toISOString()}\`
- Serial Number: \`${serialNumberContent}\`
`
const comment = require('.github/workflows/scripts/comment.js')
await comment(github, context, commentBody)
Expand Down
3 changes: 3 additions & 0 deletions seed/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ def main():
'seed_path', type=argparse.FileType('r'), nargs='?',
help='json seed file to process')
parser.add_argument('--mock_serial_number', help='mock serial number')
parser.add_argument('--version', help='version')
args = parser.parse_args()

print("Load", args.seed_path.name)
Expand All @@ -343,6 +344,8 @@ def main():
if args.mock_serial_number is not None:
seed_message.serial_number = args.mock_serial_number
update_serial_number(seed_message.serial_number)
if args.version is not None:
seed_message.version = args.version

# Serialize and save as seed file
with open(SEED_BIN_PATH, "wb") as seed_file:
Expand Down
34 changes: 34 additions & 0 deletions src/seed_tools/commands/create_seed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as fs_sync from 'fs';
import { promises as fs } from 'fs';
import * as os from 'os';
import * as path from 'path';
import { VariationsSeed } from 'src/proto/generated/variations_seed';
import { wsPath } from '../../base/path_utils';
import create_seed from './create_seed';

Expand Down Expand Up @@ -52,10 +53,43 @@ describe('create_seed command', () => {

const outputSerialNumber = await fs.readFile(serialNumberPath, 'utf-8');
expect(outputSerialNumber).toEqual('1');
expect(VariationsSeed.fromBinary(output).version).toEqual('1');
},
);
});

test('set seed version', async () => {
const testCaseDir = path.join(testDataDir, 'set_seed_version');
const studiesDir = path.join(testCaseDir, 'studies');
const outputFile = path.join(tempDir, 'output.bin');
const serialNumberPath = path.join(tempDir, 'serial_number.txt');

await create_seed.parseAsync([
'node',
'create_seed',
studiesDir,
outputFile,
'--version',
'test version value',
'--mock_serial_number',
'1',
'--serial_number_path',
serialNumberPath,
]);

const output = await fs.readFile(outputFile);
const expectedOutput = await fs.readFile(
path.join(testCaseDir, 'expected_seed.bin'),
);
expect(output).toEqual(expectedOutput);

const outputSerialNumber = await fs.readFile(serialNumberPath, 'utf-8');
expect(outputSerialNumber).toEqual('1');
expect(VariationsSeed.fromBinary(output).version).toEqual(
'test version value',
);
});

describe('invalid studies', () => {
const invalidStudiesDir = path.join(testDataDir, 'invalid_studies');
it.each(fs_sync.readdirSync(invalidStudiesDir))(
Expand Down
4 changes: 3 additions & 1 deletion src/seed_tools/commands/create_seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default new Command('create_seed')
.description('Create seed.bin from study files')
.argument('<studies_dir>', 'path to a directory containing study files')
.argument('<output_file>', 'output seed file')
.option('--version <value>', 'version to set into the seed')
.option(
'--serial_number_path <path>',
'file path to write the serial number to',
Expand All @@ -32,6 +33,7 @@ export default new Command('create_seed')
interface Options {
mock_serial_number?: string;
serial_number_path?: string;
version?: string;
}

async function main(studiesDir: string, outputFile: string, options: Options) {
Expand All @@ -47,7 +49,7 @@ async function main(studiesDir: string, outputFile: string, options: Options) {
const variationsSeed: VariationsSeed = {
study: [],
layers: [],
version: '1',
version: options.version ?? '1',
};

for (const file of files) {
Expand Down
Binary file added src/test/data/set_seed_version/expected_seed.bin
Binary file not shown.
43 changes: 43 additions & 0 deletions src/test/data/set_seed_version/studies/TestStudy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
{
"name": "TestStudy",
"experiment": [
{
"name": "Enabled",
"probability_weight": 100,
"feature_association": {
"enable_feature": [
"TestFeature"
]
}
},
{
"name": "Disabled",
"probability_weight": 0,
"feature_association": {
"disable_feature": [
"TestFeature"
]
}
},
{
"name": "Default",
"probability_weight": 0
}
],
"filter": {
"min_version": "92.1.30.57",
"channel": [
"NIGHTLY",
"BETA",
"RELEASE"
],
"platform": [
"PLATFORM_WINDOWS",
"PLATFORM_MAC",
"PLATFORM_LINUX",
"PLATFORM_ANDROID"
]
}
}
]

0 comments on commit a06d532

Please sign in to comment.